From: Soren Stoutner Date: Wed, 13 Dec 2017 23:50:00 +0000 (-0700) Subject: Fix the creation of home screen shortcuts for API >= 26. https://redmine.stoutner... X-Git-Tag: v2.8~7 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=1664a6fbaff81fd23a12d5e51ad61616bd0f69bb;p=PrivacyBrowserAndroid.git Fix the creation of home screen shortcuts for API >= 26. https://redmine.stoutner.com/issues/231 --- diff --git a/app/build.gradle b/app/build.gradle index 90c6f4bb..75255675 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,8 +66,8 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:design:26.1.0' - // Only compile `com.google.firebase:firebase-ads:9.8.0` for the free flavor. - freeImplementation 'com.google.firebase:firebase-ads:11.6.0' + // Only compile `com.google.firebase:firebase-ads` for the free flavor. + freeImplementation 'com.google.firebase:firebase-ads:11.6.2' } // Google's documentation says the following line is required for `firebase-ads` but things work correctly without it. I have no interest in applying the Google Mobile Services plugin in the standard flavor if I don't have to. diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 8e2f6e5d..63225a08 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -54,6 +54,10 @@ import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; +// `ShortcutInfoCompat`, `ShortcutManagerCompat`, and `IconCompat` can be switched to the non-compat version once API >= 26. +import android.support.v4.content.pm.ShortcutInfoCompat; +import android.support.v4.content.pm.ShortcutManagerCompat; +import android.support.v4.graphics.drawable.IconCompat; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.SwipeRefreshLayout; @@ -2420,21 +2424,28 @@ public class MainWebViewActivity extends AppCompatActivity implements AddDomainD @Override public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) { - // Get shortcutNameEditText from the alert dialog. + // Get the shortcut name. EditText shortcutNameEditText = dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext); + String shortcutNameString = shortcutNameEditText.getText().toString(); - // Create the bookmark shortcut based on formattedUrlString. - Intent bookmarkShortcut = new Intent(); - bookmarkShortcut.setAction(Intent.ACTION_VIEW); - bookmarkShortcut.setData(Uri.parse(formattedUrlString)); - - // Place the bookmark shortcut on the home screen. - Intent placeBookmarkShortcut = new Intent(); - placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.INTENT", bookmarkShortcut); - placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.NAME", shortcutNameEditText.getText().toString()); - placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.ICON", favoriteIconBitmap); - placeBookmarkShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); - sendBroadcast(placeBookmarkShortcut); + // Convert the favorite icon bitmap to an `Icon`. `IconCompat` is required until API >= 26. + IconCompat favoriteIcon = IconCompat.createWithBitmap(favoriteIconBitmap); + + // Setup the shortcut intent. + Intent shortcutIntent = new Intent(); + shortcutIntent.setAction(Intent.ACTION_VIEW); + shortcutIntent.setData(Uri.parse(formattedUrlString)); + + // Create a shortcut info builder. The shortcut name becomes the shortcut ID. + ShortcutInfoCompat.Builder shortcutInfoBuilder = new ShortcutInfoCompat.Builder(this, shortcutNameString); + + // Add the required fields to the shortcut info builder. + shortcutInfoBuilder.setIcon(favoriteIcon); + shortcutInfoBuilder.setIntent(shortcutIntent); + shortcutInfoBuilder.setShortLabel(shortcutNameString); + + // Request the pin. `ShortcutManagerCompat` can be switched to `ShortcutManager` once API >= 26. + ShortcutManagerCompat.requestPinShortcut(this, shortcutInfoBuilder.build(), null); } @Override diff --git a/app/src/main/res/menu/webview_options_menu.xml b/app/src/main/res/menu/webview_options_menu.xml index e07807b1..2761db63 100644 --- a/app/src/main/res/menu/webview_options_menu.xml +++ b/app/src/main/res/menu/webview_options_menu.xml @@ -74,19 +74,19 @@ @@ -94,7 +94,7 @@ @@ -102,49 +102,49 @@ @@ -153,36 +153,36 @@ \ No newline at end of file diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index f8ea840f..52245357 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -121,9 +121,10 @@ Cookies de terceras partes Almacenamiento DOM Datos de formulario - Borrar cookies - Borrar almacenamiento DOM - Borrar datos de formulario + Borrar datos + Borrar cookies + Borrar almacenamiento DOM + Borrar datos de formulario Tamaño de fuente 25% 50% diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 79411bc2..f7971836 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -123,9 +123,10 @@ Cookies di terze parti DOM storage Dati dei moduli - Elimina cookies - Elimina DOM storage - Elimina dati dei moduli + Elimina dati + Elimina cookies + Elimina DOM storage + Elimina dati dei moduli Dimensione font 25% 50% diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c3dfe0d9..8b7f03a6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -126,9 +126,9 @@ DOM Storage Form Data Clear Data - Clear Cookies - Clear DOM Storage - Clear Form Data + Clear Cookies + Clear DOM Storage + Clear Form Data Font Size 25% 50%