MainActivity: implement auto freeze shortcut

This commit is contained in:
Peter Cai 2018-08-23 19:50:14 +08:00
parent 960b0e4912
commit cbd6bba350
No known key found for this signature in database
GPG key ID: 71F5FB4E4F3FD54F
19 changed files with 77 additions and 28 deletions

View file

@ -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());
}
}

View file

@ -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);
}

View file

@ -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");
}
}
}

View file

@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="206.4972"
android:viewportHeight="206.50017">
<group android:translateX="68.54559"
android:translateY="68.546585">
<path
android:pathData="M32.949,34.211H3.841c-1.139,0 -2.164,-0.687 -2.598,-1.741c-0.43,-1.053 -0.178,-2.251 0.629,-3.054L26.1,5.604H3.841c-1.55,0 -2.802,-1.267 -2.802,-2.802C1.04,1.248 2.292,0 3.841,0h29.108c1.137,0 2.164,0.689 2.593,1.743c0.432,1.053 0.18,2.252 -0.627,3.053L10.687,28.61h22.263c1.545,0 2.802,1.261 2.802,2.802C35.751,32.963 34.49,34.211 32.949,34.211zM65.562,39.967H51.849l15.688,-15.42c0.812,-0.807 1.051,-2 0.63,-3.053c-0.43,-1.053 -1.466,-1.747 -2.604,-1.747H44.995c-1.538,0 -2.802,1.255 -2.802,2.801c0,1.535 1.264,2.802 2.802,2.802h13.721L43.027,40.766c-0.818,0.81 -1.058,1.998 -0.627,3.054c0.434,1.062 1.456,1.751 2.595,1.751h20.566c1.549,0 2.802,-1.259 2.802,-2.802C68.363,41.225 67.11,39.967 65.562,39.967zM33.935,63.804h-9.565L35.9,52.477c0.814,-0.799 1.06,-2.009 0.63,-3.063c-0.432,-1.052 -1.459,-1.741 -2.596,-1.741H17.524c-1.547,0 -2.803,1.258 -2.803,2.802c0,1.542 1.256,2.802 2.803,2.802h9.565L15.557,64.602c-0.812,0.81 -1.06,1.998 -0.627,3.054c0.431,1.056 1.456,1.751 2.595,1.751h16.411c1.547,0 2.801,-1.259 2.801,-2.802S35.494,63.804 33.935,63.804z"
android:fillColor="#FFFFFF"/>
</group>
</vector>

View file

@ -3,4 +3,8 @@
<item
android:id="@+id/main_menu_freeze_all"
android:title="@string/freeze_all" />
<item
android:id="@+id/main_menu_create_freeze_all_shortcut"
android:title="@string/create_freeze_all_shortcut" />
</menu>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_freeze_background"/>
<foreground android:drawable="@drawable/ic_freeze_foreground"/>
</adaptive-icon>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_freeze_background"/>
<foreground android:drawable="@drawable/ic_freeze_foreground"/>
</adaptive-icon>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_freeze_background">#26A69A</color>
</resources>

View file

@ -25,6 +25,8 @@
<string name="unfreeze_and_launch">Unfreeze and Launch</string>
<string name="auto_freeze">Auto Freeze</string>
<string name="freeze_all">Batch Freeze</string>
<string name="create_freeze_all_shortcut">Create Batch Freeze Shortcut</string>
<string name="freeze_all_shortcut">Freeze</string>
<string name="clone_success">Application "%s" cloned successfully</string>
<string name="uninstall_success">Application "%s" uninstalled successfully</string>
<string name="freeze_success">Application "%s" frozen successfully</string>