This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
499 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
...ure/pwa/src/main/java/mozilla/components/feature/pwa/feature/WebAppSiteControlsFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.feature.pwa.feature | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.BroadcastReceiver | ||
import android.content.ClipData | ||
import android.content.ClipboardManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.Build | ||
import android.widget.Toast | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationCompat.BADGE_ICON_NONE | ||
import androidx.core.app.NotificationManagerCompat | ||
import androidx.core.content.getSystemService | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleObserver | ||
import androidx.lifecycle.OnLifecycleEvent | ||
import mozilla.components.browser.session.SessionManager | ||
import mozilla.components.concept.engine.manifest.WebAppManifest | ||
import mozilla.components.feature.pwa.R | ||
import mozilla.components.feature.session.SessionUseCases | ||
import mozilla.components.support.base.ids.cancel | ||
import mozilla.components.support.base.ids.notify | ||
|
||
/** | ||
* Displays site controls notification for fullscreen web apps. | ||
*/ | ||
class WebAppSiteControlsFeature( | ||
private val applicationContext: Context, | ||
private val sessionManager: SessionManager, | ||
private val reloadUrlUseCase: SessionUseCases.ReloadUrlUseCase, | ||
private val sessionId: String, | ||
private val manifest: WebAppManifest? | ||
) : BroadcastReceiver(), LifecycleObserver { | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | ||
fun onResume() { | ||
val filter = IntentFilter().apply { | ||
addAction(ACTION_COPY) | ||
addAction(ACTION_REFRESH) | ||
} | ||
applicationContext.registerReceiver(this, filter) | ||
|
||
NotificationManagerCompat.from(applicationContext) | ||
.notify(applicationContext, NOTIFICATION_TAG, buildNotification()) | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) | ||
fun onPause() { | ||
applicationContext.unregisterReceiver(this) | ||
|
||
NotificationManagerCompat.from(applicationContext) | ||
.cancel(applicationContext, NOTIFICATION_TAG) | ||
} | ||
|
||
/** | ||
* Responds to [PendingIntent]s fired by the site controls notification. | ||
*/ | ||
override fun onReceive(context: Context, intent: Intent) { | ||
sessionManager.findSessionById(sessionId)?.also { session -> | ||
when (intent.action) { | ||
ACTION_COPY -> { | ||
applicationContext.getSystemService<ClipboardManager>()?.let { clipboardManager -> | ||
clipboardManager.setPrimaryClip(ClipData.newPlainText(session.url, session.url)) | ||
Toast.makeText( | ||
applicationContext, | ||
applicationContext.getString(R.string.mozac_feature_pwa_copy_success), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
} | ||
ACTION_REFRESH -> reloadUrlUseCase(session) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Build the notification with site controls to be displayed while the web app is active. | ||
*/ | ||
@Suppress("LongMethod") | ||
private fun buildNotification(): Notification { | ||
val channelId = ensureChannelExists() | ||
|
||
with(applicationContext) { | ||
val copyIntent = createPendingIntent(ACTION_COPY, 1) | ||
val refreshAction = NotificationCompat.Action( | ||
R.drawable.ic_refresh, | ||
getString(R.string.mozac_feature_pwa_site_controls_refresh), | ||
createPendingIntent(ACTION_REFRESH, 2) | ||
) | ||
|
||
return NotificationCompat.Builder(this, channelId) | ||
.setSmallIcon(R.drawable.ic_pwa) | ||
.setContentTitle(manifest?.name) | ||
.setContentText(getString(R.string.mozac_feature_pwa_site_controls_notification_text)) | ||
.setBadgeIconType(BADGE_ICON_NONE) | ||
.setColor(manifest?.themeColor ?: NotificationCompat.COLOR_DEFAULT) | ||
.setPriority(NotificationCompat.PRIORITY_MIN) | ||
.setShowWhen(false) | ||
.setContentIntent(copyIntent) | ||
.addAction(refreshAction) | ||
.setOngoing(true) | ||
.build() | ||
} | ||
} | ||
|
||
/** | ||
* Make sure a notification channel for site controls notifications exists. | ||
* | ||
* Returns the channel id to be used for notifications. | ||
*/ | ||
private fun ensureChannelExists(): String { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val notificationManager: NotificationManager = applicationContext.getSystemService()!! | ||
|
||
val channel = NotificationChannel( | ||
NOTIFICATION_CHANNEL_ID, | ||
applicationContext.getString(R.string.mozac_feature_pwa_site_controls_notification_channel), | ||
NotificationManager.IMPORTANCE_MIN | ||
) | ||
|
||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
return NOTIFICATION_CHANNEL_ID | ||
} | ||
|
||
private fun createPendingIntent(action: String, requestCode: Int): PendingIntent { | ||
val intent = Intent(action) | ||
intent.setPackage(applicationContext.packageName) | ||
return PendingIntent.getBroadcast(applicationContext, requestCode, intent, 0) | ||
} | ||
|
||
companion object { | ||
private const val NOTIFICATION_CHANNEL_ID = "Site Controls" | ||
private const val NOTIFICATION_TAG = "SiteControls" | ||
private const val ACTION_COPY = "mozilla.components.feature.pwa.COPY" | ||
private const val ACTION_REFRESH = "mozilla.components.feature.pwa.REFRESH" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- This Source Code Form is subject to the terms of the Mozilla Public | ||
- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M16.72 14.42l0.58-1.46h1.67l-0.8-2.22 1-2.5L22 15.76h-2.1l-0.48-1.35h-2.7zM14.94 15.77l3.03-7.54h-2.01l-2.08 4.87-1.48-4.86h-1.54L9.27 13.1l-1.12-2.22-1 3.12 1.02 1.77h1.98l1.43-4.37 1.37 4.37h1.99zM3.91 13.18h1.24c0.38 0 0.71-0.04 1-0.13l0.32-0.98 0.9-2.76A2.2 2.2 0 0 0 7.14 9c-0.46-0.51-1.14-0.77-2.02-0.77H2v7.54h1.91v-2.59zm1.64-3.21c0.18 0.18 0.27 0.42 0.27 0.72s-0.08 0.55-0.24 0.73c-0.17 0.2-0.49 0.3-0.95 0.3H3.9V9.7h0.72c0.44 0 0.74 0.09 0.92 0.27z"/> | ||
</vector> |
13 changes: 13 additions & 0 deletions
13
components/feature/pwa/src/main/res/drawable/ic_refresh.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- This Source Code Form is subject to the terms of the Mozilla Public | ||
- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="@android:color/white" | ||
android:pathData="M21,4.04a0.96,0.96 0,0 0,-0.96 0.96V8a8.981,8.981 0,1 0,-1.676 10.361A1,1 0,0 0,16.95 16.95a7.031,7.031 0,1 1,1.72 -6.91H15a0.96,0.96 0,0 0,0 1.92h6a0.96,0.96 0,0 0,0.96 -0.96V5A0.96,0.96 0,0 0,21 4.04Z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...pwa/src/test/java/mozilla/components/feature/pwa/feature/WebAppSiteControlsFeatureTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.feature.pwa.feature | ||
|
||
import android.content.Intent | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.browser.session.Session | ||
import mozilla.components.browser.session.SessionManager | ||
import mozilla.components.feature.session.SessionUseCases | ||
import mozilla.components.support.test.any | ||
import mozilla.components.support.test.eq | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.doNothing | ||
import org.mockito.Mockito.doReturn | ||
import org.mockito.Mockito.spy | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class WebAppSiteControlsFeatureTest { | ||
|
||
@Test | ||
fun `register receiver on resume`() { | ||
val context = spy(testContext) | ||
|
||
val feature = WebAppSiteControlsFeature(context, mock(), mock(), "session-id", mock()) | ||
feature.onResume() | ||
|
||
verify(context).registerReceiver(eq(feature), any()) | ||
} | ||
|
||
@Test | ||
fun `unregister receiver on pause`() { | ||
val context = spy(testContext) | ||
|
||
doNothing().`when`(context).unregisterReceiver(any()) | ||
|
||
val feature = WebAppSiteControlsFeature(context, mock(), mock(), "session-id", mock()) | ||
feature.onPause() | ||
|
||
verify(context).unregisterReceiver(feature) | ||
} | ||
|
||
@Test | ||
fun `reload page when reload action is activated`() { | ||
val sessionManager: SessionManager = mock() | ||
val session: Session = mock() | ||
val reloadUrlUseCase: SessionUseCases.ReloadUrlUseCase = mock() | ||
|
||
doReturn(session).`when`(sessionManager).findSessionById("session-id") | ||
|
||
val feature = WebAppSiteControlsFeature(testContext, sessionManager, reloadUrlUseCase, "session-id", mock()) | ||
feature.onReceive(testContext, Intent("mozilla.components.feature.pwa.REFRESH")) | ||
|
||
verify(reloadUrlUseCase).invoke(session) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.