Fix the creation of home screen shortcuts for API >= 26. https://redmine.stoutner...
authorSoren Stoutner <soren@stoutner.com>
Wed, 13 Dec 2017 23:50:00 +0000 (16:50 -0700)
committerSoren Stoutner <soren@stoutner.com>
Wed, 13 Dec 2017 23:50:00 +0000 (16:50 -0700)
app/build.gradle
app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
app/src/main/res/menu/webview_options_menu.xml
app/src/main/res/values-es/strings.xml
app/src/main/res/values-it/strings.xml
app/src/main/res/values/strings.xml

index 90c6f4bb037b165b89eb6304dbcbb1ac40dc6ce1..7525567505167d439cdf740fd883f802fb9c1662 100644 (file)
@@ -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.
index 8e2f6e5d3bab3c205c565e112ba96836b077be22..63225a08f55b59253d1da33de4b92e803930f8b2 100644 (file)
@@ -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
index e07807b166177cae5c797d0f5bf4f05fd0efdd32..2761db63311a974255169c1dc31d9ee163d0c29a 100644 (file)
             <item
                 android:id="@+id/clear_cookies"
                 android:title="@string/clear_cookies"
-                android:orderInCategory="70"
+                android:orderInCategory="71"
                 app:showAsAction="never" />
 
             <item
                 android:id="@+id/clear_dom_storage"
                 android:title="@string/clear_dom_storage"
-                android:orderInCategory="80"
+                android:orderInCategory="72"
                 app:showAsAction="never" />
 
             <item
                 android:id="@+id/clear_form_data"
                 android:title="@string/clear_form_data"
-                android:orderInCategory="90"
+                android:orderInCategory="73"
                 app:showAsAction="never" />
         </menu>
     </item>
@@ -94,7 +94,7 @@
     <item
         android:id="@+id/font_size"
         android:title="@string/font_size"
-        android:orderInCategory="100"
+        android:orderInCategory="80"
         app:showAsAction="never" >
 
         <menu>
                 <item
                     android:id="@+id/font_size_twenty_five_percent"
                     android:title="@string/twenty_five_percent"
-                    android:orderInCategory="101"
+                    android:orderInCategory="81"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_fifty_percent"
                     android:title="@string/fifty_percent"
-                    android:orderInCategory="102"
+                    android:orderInCategory="82"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_seventy_five_percent"
                     android:title="@string/seventy_five_percent"
-                    android:orderInCategory="103"
+                    android:orderInCategory="83"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_one_hundred_percent"
                     android:title="@string/one_hundred_percent"
-                    android:orderInCategory="104"
+                    android:orderInCategory="84"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_one_hundred_twenty_five_percent"
                     android:title="@string/one_hundred_twenty_five_percent"
-                    android:orderInCategory="105"
+                    android:orderInCategory="85"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_one_hundred_fifty_percent"
                     android:title="@string/one_hundred_fifty_percent"
-                    android:orderInCategory="106"
+                    android:orderInCategory="86"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_one_hundred_seventy_five_percent"
                     android:title="@string/one_hundred_seventy_five_percent"
-                    android:orderInCategory="107"
+                    android:orderInCategory="87"
                     app:showAsAction="never" />
 
                 <item
                     android:id="@+id/font_size_two_hundred_percent"
                     android:title="@string/two_hundred_percent"
-                    android:orderInCategory="108"
+                    android:orderInCategory="88"
                     app:showAsAction="never" />
             </group>
         </menu>
     <item
         android:id="@+id/display_images"
         android:title="@string/display_images"
-        android:orderInCategory="110"
+        android:orderInCategory="90"
         android:checkable="true"
         app:showAsAction="never" />
 
     <item
         android:id="@+id/share"
         android:title="@string/share"
-        android:orderInCategory="120"
+        android:orderInCategory="100"
         app:showAsAction="never" />
 
     <item
         android:id="@+id/find_on_page"
         android:title="@string/find_on_page"
-        android:orderInCategory="130"
+        android:orderInCategory="110"
         app:showAsAction="never|collapseActionView" />
     <item
         android:id="@+id/print"
         android:title="@string/print"
-        android:orderInCategory="140"
+        android:orderInCategory="120"
         app:showAsAction="never" />
 
     <item
         android:id="@+id/add_to_homescreen"
         android:title="@string/add_to_home_screen"
-        android:orderInCategory="150"
+        android:orderInCategory="130"
         app:showAsAction="never" />
 
     <item
         android:id="@+id/refresh"
         android:title="@string/refresh"
-        android:orderInCategory="160"
+        android:orderInCategory="140"
         app:showAsAction="never" />
 </menu>
\ No newline at end of file
index f8ea840f2ad6083a57812cdd51e31c82856bc6e5..52245357ea5fdaa2590f7ccbafe4fd243af94244 100644 (file)
     <string name="third_party_cookies">Cookies de terceras partes</string>
     <string name="dom_storage">Almacenamiento DOM</string>
     <string name="form_data">Datos de formulario</string>
-    <string name="clear_cookies">Borrar cookies</string>
-    <string name="clear_dom_storage">Borrar almacenamiento DOM</string>
-    <string name="clear_form_data">Borrar datos de formulario</string>
+    <string name="clear_data">Borrar datos</string>
+        <string name="clear_cookies">Borrar cookies</string>
+        <string name="clear_dom_storage">Borrar almacenamiento DOM</string>
+        <string name="clear_form_data">Borrar datos de formulario</string>
     <string name="font_size">TamaƱo de fuente</string>
         <string name="twenty_five_percent">25%</string>
         <string name="fifty_percent">50%</string>
index 79411bc2d99b132684f12e430883e794afbfa83f..f79718361ac53b66fe911a8a35a2d90c1007ce05 100644 (file)
     <string name="third_party_cookies">Cookies di terze parti</string>
     <string name="dom_storage">DOM storage</string>
     <string name="form_data">Dati dei moduli</string>
-    <string name="clear_cookies">Elimina cookies</string>
-    <string name="clear_dom_storage">Elimina DOM storage</string>
-    <string name="clear_form_data">Elimina dati dei moduli</string>
+    <string name="clear_data">Elimina dati</string>
+        <string name="clear_cookies">Elimina cookies</string>
+        <string name="clear_dom_storage">Elimina DOM storage</string>
+        <string name="clear_form_data">Elimina dati dei moduli</string>
     <string name="font_size">Dimensione font</string>
         <string name="twenty_five_percent">25%</string>
         <string name="fifty_percent">50%</string>
index c3dfe0d98af7cf4b1790c3269f8508e47b113708..8b7f03a6f7e4e9043c5aaf1ca979ed73197827b5 100644 (file)
     <string name="dom_storage">DOM Storage</string>
     <string name="form_data">Form Data</string>
     <string name="clear_data">Clear Data</string>
-    <string name="clear_cookies">Clear Cookies</string>
-    <string name="clear_dom_storage">Clear DOM Storage</string>
-    <string name="clear_form_data">Clear Form Data</string>
+        <string name="clear_cookies">Clear Cookies</string>
+        <string name="clear_dom_storage">Clear DOM Storage</string>
+        <string name="clear_form_data">Clear Form Data</string>
     <string name="font_size">Font Size</string>
         <string name="twenty_five_percent">25%</string>
         <string name="fifty_percent">50%</string>