Handle pin item request in shortcut creation

This commit is contained in:
Pierrot 2019-02-27 08:50:54 +01:00
parent eb71ae68bf
commit 49f08d35e8
4 changed files with 75 additions and 42 deletions

View file

@ -3325,25 +3325,12 @@ public class Dashboard extends ResourceWrapperActivity implements OnLongClickLis
@TargetApi(Build.VERSION_CODES.O)
private void addPinnedShortcut(Intent intent) {
Parcelable extra = intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST);
if(extra instanceof LauncherApps.PinItemRequest) {
LauncherApps.PinItemRequest request = (LauncherApps.PinItemRequest) extra;
if (request.getRequestType() == LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT) {
final LauncherApps launcherApps = (LauncherApps) getSystemService(LAUNCHER_APPS_SERVICE);
ShortcutInfo shortcutInfo = request.getShortcutInfo();
final Drawable iconDrawable = launcherApps.getShortcutIconDrawable(shortcutInfo, Utils.getLauncherIconDensity());
Bitmap icon = Utils.createBitmapFromDrawable(iconDrawable);
Intent si = new Intent(Shortcut.INTENT_ACTION_APP_SHORTCUT);
si.putExtra(Shortcut.INTENT_EXTRA_APP_SHORTCUT_ID, shortcutInfo.getId());
si.putExtra(Shortcut.INTENT_EXTRA_APP_SHORTCUT_PKG, shortcutInfo.getPackage());
si.putExtra(Shortcut.INTENT_EXTRA_APP_SHORTCUT_DISABLED_MSG, shortcutInfo.getDisabledMessage());
Utils.ShortcutDescription sd = Utils.createPinItemRequestFromIntent(this, intent);
if(sd != null) {
final ItemLayout il = mScreen.getTargetOrTopmostItemLayout();
Page page = il.getPage();
float scale = il.getCurrentScale();
final Item newItem = Utils.addShortcut(shortcutInfo.getShortLabel().toString(), icon, si, page, Utils.POSITION_AUTO, Utils.POSITION_AUTO, scale, true);
final Item newItem = Utils.addShortcut(sd.name, sd.icon, sd.intent, page, Utils.POSITION_AUTO, Utils.POSITION_AUTO, scale, true);
mUndoStack.storePageAddItem(newItem);
editItem(il, newItem);
@ -3353,9 +3340,6 @@ public class Dashboard extends ResourceWrapperActivity implements OnLongClickLis
mScreen.ensureItemViewVisible(il.getItemView(newItem), false);
}
}, 1000);
request.accept();
}
}
}

View file

@ -61,7 +61,7 @@ public class ShortcutReceiver extends BroadcastReceiver {
if(duplicate || found==null) {
int icon_size=(int)(home_page.config.defaultShortcutConfig.iconScale* Utils.getStandardIconSize());
Utils.ShortcutDescription sd=Utils.createShortcutFromIntent(context, data, icon_size);
if(sd != null) {
int id = home_page.findFreeItemId();
int[] cell = Utils.findFreeCell(home_page);
Shortcut shortcut = new Shortcut(home_page);
@ -77,6 +77,7 @@ public class ShortcutReceiver extends BroadcastReceiver {
home_page.addItem(shortcut);
}
}
}
private void uninstallShortcut(Context context, Intent data) {
// TODO: implement uninstall shortcut

View file

@ -1,5 +1,6 @@
package net.pierrox.lightning_launcher.data;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
@ -11,9 +12,11 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.LauncherApps;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ShortcutInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@ -1032,6 +1035,10 @@ public class Utils {
public static ShortcutDescription createShortcutFromIntent(Context context, Intent data, int max_icon_size) {
// this comes from Launcher2
Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
if(intent == null) {
return null;
}
String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
@ -1062,6 +1069,37 @@ public class Utils {
return sd;
}
@TargetApi(Build.VERSION_CODES.O)
public static ShortcutDescription createPinItemRequestFromIntent(Context context, Intent intent) {
Parcelable extra = intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST);
if(extra instanceof LauncherApps.PinItemRequest) {
LauncherApps.PinItemRequest request = (LauncherApps.PinItemRequest) extra;
if (request.getRequestType() == LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT) {
final LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
ShortcutInfo shortcutInfo = request.getShortcutInfo();
final Drawable iconDrawable = launcherApps.getShortcutIconDrawable(shortcutInfo, Utils.getLauncherIconDensity());
Bitmap icon = Utils.createBitmapFromDrawable(iconDrawable);
Intent si = new Intent(Shortcut.INTENT_ACTION_APP_SHORTCUT);
si.putExtra(Shortcut.INTENT_EXTRA_APP_SHORTCUT_ID, shortcutInfo.getId());
si.putExtra(Shortcut.INTENT_EXTRA_APP_SHORTCUT_PKG, shortcutInfo.getPackage());
si.putExtra(Shortcut.INTENT_EXTRA_APP_SHORTCUT_DISABLED_MSG, shortcutInfo.getDisabledMessage());
ShortcutDescription sd=new ShortcutDescription();
sd.name = shortcutInfo.getShortLabel().toString();
sd.icon = icon;
sd.intent = si;
request.accept();
return sd;
}
}
return null;
}
public static void deleteDirectory(File dir, boolean delete_root) {
File[] files=dir.listFiles();
if(files!=null) {
@ -1479,7 +1517,17 @@ public class Utils {
public static Item addAndroidShortcutFromIntent(Context context, Intent data, Page page, int x, int y, float scale) {
int icon_size=(int)(page.config.defaultShortcutConfig.iconScale*getStandardIconSize());
ShortcutDescription sd=createShortcutFromIntent(context, data, icon_size);
if(sd == null) {
sd = createPinItemRequestFromIntent(context, data);
}
if(sd == null) {
sd = new ShortcutDescription();
sd.name = "no shortcut";
sd.intent = new Intent();
}
return addShortcut(sd.name, sd.icon, sd.intent, page, x, y, scale, true);
}

View file

@ -652,7 +652,7 @@ public class LightningEngine implements Page.PageListener {
if (item.getClass() == Shortcut.class) {
Shortcut shortcut = (Shortcut) item;
Intent intent = shortcut.getIntent();
if (Shortcut.INTENT_ACTION_APP_SHORTCUT.equals(intent.getAction())) {
if (intent != null && Shortcut.INTENT_ACTION_APP_SHORTCUT.equals(intent.getAction())) {
return intent;
}
}