diff --git a/app/src/main/java/net/typeblog/shelter/ui/AppListFragment.java b/app/src/main/java/net/typeblog/shelter/ui/AppListFragment.java index 906bbfa..b10e0ee 100644 --- a/app/src/main/java/net/typeblog/shelter/ui/AppListFragment.java +++ b/app/src/main/java/net/typeblog/shelter/ui/AppListFragment.java @@ -1,18 +1,14 @@ package net.typeblog.shelter.ui; import android.app.Activity; -import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.ShortcutInfo; -import android.content.pm.ShortcutManager; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.graphics.drawable.Icon; -import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; @@ -331,28 +327,8 @@ public class AppListFragment extends Fragment { launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Then tell the launcher to add the shortcut - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - ShortcutManager shortcutManager = getContext().getSystemService(ShortcutManager.class); - - if (shortcutManager.isRequestPinShortcutSupported()) { - ShortcutInfo info = new ShortcutInfo.Builder(getContext(), "shelter-" + app.getPackageName()) - .setIntent(launchIntent) - .setIcon(Icon.createWithBitmap(icon)) - .setShortLabel(app.getLabel()) - .setLongLabel(app.getLabel()) - .build(); - Intent addIntent = shortcutManager.createShortcutResultIntent(info); - shortcutManager.requestPinShortcut(info, - PendingIntent.getBroadcast(getContext(), 0, addIntent, 0).getIntentSender()); - } else { - // TODO: Maybe implement this for launchers without pin shortcut support? - // TODO: Should be the same with the fallback for Android < O - // for now just show unsupported - Toast.makeText(getContext(), getString(R.string.unsupported_launcher), Toast.LENGTH_LONG).show(); - } - } else { - // TODO: Maybe backport for Android < O? - throw new RuntimeException("unimplemented"); - } + Utility.createLauncherShortcut(getContext(), launchIntent, + Icon.createWithBitmap(icon), "shelter-" + app.getPackageName(), + app.getLabel()); } } diff --git a/app/src/main/java/net/typeblog/shelter/ui/MainActivity.java b/app/src/main/java/net/typeblog/shelter/ui/MainActivity.java index 1cb10ff..908b522 100644 --- a/app/src/main/java/net/typeblog/shelter/ui/MainActivity.java +++ b/app/src/main/java/net/typeblog/shelter/ui/MainActivity.java @@ -4,6 +4,7 @@ import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; +import android.graphics.drawable.Icon; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; @@ -231,7 +232,14 @@ public class MainActivity extends AppCompatActivity { intent.setComponent(new ComponentName(this, DummyActivity.class)); startActivity(intent); return true; - // TODO: batch freeze shortcut + case R.id.main_menu_create_freeze_all_shortcut: + Intent launchIntent = new Intent(DummyActivity.PUBLIC_FREEZE_ALL); + launchIntent.setComponent(new ComponentName(this, DummyActivity.class)); + launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + Utility.createLauncherShortcut(this, launchIntent, + Icon.createWithResource(this, R.mipmap.ic_freeze), + "shelter-freeze-all", getString(R.string.freeze_all_shortcut)); + return true; } return super.onOptionsItemSelected(item); } diff --git a/app/src/main/java/net/typeblog/shelter/util/Utility.java b/app/src/main/java/net/typeblog/shelter/util/Utility.java index 98dac50..0de4910 100644 --- a/app/src/main/java/net/typeblog/shelter/util/Utility.java +++ b/app/src/main/java/net/typeblog/shelter/util/Utility.java @@ -1,5 +1,6 @@ package net.typeblog.shelter.util; +import android.app.PendingIntent; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; @@ -7,12 +8,18 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.content.pm.ShortcutInfo; +import android.content.pm.ShortcutManager; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.graphics.drawable.Icon; +import android.os.Build; import android.os.UserManager; +import android.widget.Toast; +import net.typeblog.shelter.R; import net.typeblog.shelter.receivers.ShelterDeviceAdminReceiver; import net.typeblog.shelter.services.IShelterService; import net.typeblog.shelter.ui.DummyActivity; @@ -125,4 +132,30 @@ public class Utility { list.removeIf((it) -> apps.stream().noneMatch((x) -> x.getPackageName().equals(it))); LocalStorageManager.getInstance().setStringList(pref, list.toArray(new String[]{})); } + + public static void createLauncherShortcut(Context context, Intent launchIntent, Icon icon, String id, String label) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); + + if (shortcutManager.isRequestPinShortcutSupported()) { + ShortcutInfo info = new ShortcutInfo.Builder(context, id) + .setIntent(launchIntent) + .setIcon(icon) + .setShortLabel(label) + .setLongLabel(label) + .build(); + Intent addIntent = shortcutManager.createShortcutResultIntent(info); + shortcutManager.requestPinShortcut(info, + PendingIntent.getBroadcast(context, 0, addIntent, 0).getIntentSender()); + } else { + // TODO: Maybe implement this for launchers without pin shortcut support? + // TODO: Should be the same with the fallback for Android < O + // for now just show unsupported + Toast.makeText(context, context.getString(R.string.unsupported_launcher), Toast.LENGTH_LONG).show(); + } + } else { + // TODO: Maybe backport for Android < O? + throw new RuntimeException("unimplemented"); + } + } } diff --git a/app/src/main/res/drawable/ic_freeze_foreground.xml b/app/src/main/res/drawable/ic_freeze_foreground.xml new file mode 100644 index 0000000..00cb809 --- /dev/null +++ b/app/src/main/res/drawable/ic_freeze_foreground.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/app/src/main/res/menu/main_activity_menu.xml b/app/src/main/res/menu/main_activity_menu.xml index 8d8ef3c..0bfc389 100644 --- a/app/src/main/res/menu/main_activity_menu.xml +++ b/app/src/main/res/menu/main_activity_menu.xml @@ -3,4 +3,8 @@ + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_freeze.xml b/app/src/main/res/mipmap-anydpi-v26/ic_freeze.xml new file mode 100644 index 0000000..d2ac867 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_freeze.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_freeze_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_freeze_round.xml new file mode 100644 index 0000000..d2ac867 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_freeze_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_freeze.png b/app/src/main/res/mipmap-hdpi/ic_freeze.png new file mode 100644 index 0000000..63aa8fd Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_freeze.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_freeze_round.png b/app/src/main/res/mipmap-hdpi/ic_freeze_round.png new file mode 100644 index 0000000..63aa8fd Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_freeze_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_freeze.png b/app/src/main/res/mipmap-mdpi/ic_freeze.png new file mode 100644 index 0000000..8abd608 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_freeze.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_freeze_round.png b/app/src/main/res/mipmap-mdpi/ic_freeze_round.png new file mode 100644 index 0000000..8abd608 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_freeze_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_freeze.png b/app/src/main/res/mipmap-xhdpi/ic_freeze.png new file mode 100644 index 0000000..3943219 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_freeze.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_freeze_round.png b/app/src/main/res/mipmap-xhdpi/ic_freeze_round.png new file mode 100644 index 0000000..3943219 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_freeze_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_freeze.png b/app/src/main/res/mipmap-xxhdpi/ic_freeze.png new file mode 100644 index 0000000..7410e17 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_freeze.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_freeze_round.png b/app/src/main/res/mipmap-xxhdpi/ic_freeze_round.png new file mode 100644 index 0000000..7410e17 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_freeze_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_freeze.png b/app/src/main/res/mipmap-xxxhdpi/ic_freeze.png new file mode 100644 index 0000000..b1c0999 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_freeze.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_freeze_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_freeze_round.png new file mode 100644 index 0000000..b1c0999 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_freeze_round.png differ diff --git a/app/src/main/res/values/ic_freeze_background.xml b/app/src/main/res/values/ic_freeze_background.xml new file mode 100644 index 0000000..464941f --- /dev/null +++ b/app/src/main/res/values/ic_freeze_background.xml @@ -0,0 +1,4 @@ + + + #26A69A + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a6148b5..7687fe6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,6 +25,8 @@ Unfreeze and Launch Auto Freeze Batch Freeze + Create Batch Freeze Shortcut + Freeze Application "%s" cloned successfully Application "%s" uninstalled successfully Application "%s" frozen successfully