Skip to content

Commit fe8eb50

Browse files
committed
add shortcut for apps
1 parent b8281b2 commit fe8eb50

File tree

5 files changed

+87
-47
lines changed

5 files changed

+87
-47
lines changed

PowerFileExplorer/src/main/java/net/gnu/androidutil/AndroidUtils.java

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.security.SecureRandom;
4747
import javax.crypto.SecretKey;
4848
import net.gnu.common.*;
49+
import android.content.Intent.*;
4950

5051

5152
public class AndroidUtils {
@@ -66,15 +67,62 @@ public class AndroidUtils {
6667
// //displayProps = point;
6768
// return point;
6869
// }
69-
70-
70+
71+
7172
public static void startService(final Activity activity, final Class<? extends Service> service, final String action, final String tag) {
7273
Log.i(tag, "Starting service");
7374
final Intent intent = new Intent(action);//ForegroundService.ACTION_FOREGROUND);//ACTION_BACKGROUND
7475
intent.setClass(activity, service);
7576
activity.startService(intent);
7677
}
77-
78+
79+
public static boolean createShortCut(final Context context, final String packageName) {
80+
final PackageManager pm = context.getPackageManager();
81+
final Intent iMain = new Intent(Intent.ACTION_MAIN);
82+
iMain.addCategory(Intent.CATEGORY_LAUNCHER);
83+
final List<ResolveInfo> res = pm.queryIntentActivities(iMain, 0);
84+
ActivityInfo ai = null;
85+
ActivityInfo activityInfo;
86+
for (ResolveInfo ri : res) {
87+
activityInfo = ri.activityInfo;
88+
//System.out.println("the application name is: " + activityInfo.loadLabel(pm) + ", " + activityInfo.packageName + ", " + activityInfo.name);
89+
if (packageName.equalsIgnoreCase(activityInfo.packageName)) {
90+
ai = activityInfo;
91+
break;
92+
}
93+
}
94+
if (ai != null) {
95+
final Intent shortcutIntent = new Intent();
96+
shortcutIntent.setClassName(ai.packageName, ai.name);
97+
shortcutIntent.setAction(Intent.ACTION_MAIN);
98+
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
99+
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
100+
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
101+
102+
final Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
103+
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
104+
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ai.loadLabel(pm) + "");
105+
intent.putExtra("duplicate", false);
106+
107+
final Drawable d = ai.loadIcon(pm);
108+
System.out.println("d " + d);
109+
if (d != null) {
110+
final int size = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
111+
final Bitmap icon = BitmapUtil.resizeKeepScale(BitmapUtil.drawableToBitmap(d), size);
112+
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
113+
} else {
114+
final Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), R.drawable.ic_launcher);
115+
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
116+
}
117+
context.sendBroadcast(intent);
118+
//Log.d(TAG, "in the shortcutapp on create method completed");
119+
return true;
120+
} else {
121+
//Log.d(TAG, "appllicaton not found");
122+
return false;
123+
}
124+
}
125+
78126
public static void createShortCut(final Context ctx, Class<? extends Activity> activity, String name, int resIcon){
79127
//<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
80128
//<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
@@ -117,7 +165,7 @@ public static void addShortcut(final Context context, final File f) {
117165
shortcutIntent = new Intent(context.getApplicationContext(),
118166
context.getClass());
119167
}
120-
168+
121169
shortcutIntent.setData(Uri.fromFile(f));
122170
shortcutIntent.putExtra(Constants.EXTRA_ABSOLUTE_PATH, absolutePath);
123171
shortcutIntent.setAction(Intent.ACTION_MAIN);
@@ -131,34 +179,8 @@ public static void addShortcut(final Context context, final File f) {
131179
context.sendBroadcast(addIntent);
132180
}
133181

134-
// Creates shortcut on Android widget screen
135-
// public static void createShortcutIcon(Context ctx, Activity act, String shortcutName) {
136-
// String PREFS_NAME = "PREFS_NAME";
137-
// String PREF_KEY_SHORTCUT_ADDED = "PREF_KEY_SHORTCUT_ADDED";
138-
// // Checking if ShortCut was already added
139-
// final SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
140-
// final boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
141-
// if (shortCutWasAlreadyAdded)
142-
// return;
143-
//
144-
// final Intent shortcutIntent = new Intent(ctx.getApplicationContext(), act.getClass());
145-
// shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
146-
// shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
147-
//
148-
// final Intent addIntent = new Intent();
149-
// addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
150-
// addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
151-
// //addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(ctx.getApplicationContext(), R.drawable.ic_launcher));
152-
// addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
153-
// ctx.getApplicationContext().sendBroadcast(addIntent);
154-
//
155-
// // Remembering that ShortCut was already added
156-
// final SharedPreferences.Editor editor = sharedPreferences.edit();
157-
// editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
158-
// editor.commit();
159-
// }
160-
161182

183+
162184
public static void scanMedia(final Context ctx, final String root, final boolean includeSubFolder) {
163185
if (Build.VERSION.SDK_INT >= 19) {
164186
final Collection<File> fs = FileUtil.getFiles(root, includeSubFolder);
@@ -427,7 +449,7 @@ public static StringBuilder getSignature(final Context context, final String apk
427449
sb.append("\nCertificate version: ").append(x509Cert.getVersion());
428450
sb.append("\nCertificate not before: ").append(x509Cert.getNotBefore());
429451
sb.append("\nCertificate not after: ").append(x509Cert.getNotAfter());
430-
452+
431453
} catch (CertificateException e) {
432454
e.printStackTrace();
433455
} finally {
@@ -1138,7 +1160,7 @@ public static void investigateApps(Context ctx) {
11381160
Log.d(TAG, "Package Name :" + applicationInfo.packageName);
11391161

11401162
Log.d(TAG, "Launch Intent For Package :" +
1141-
pm.getLaunchIntentForPackage(applicationInfo.packageName));
1163+
pm.getLaunchIntentForPackage(applicationInfo.packageName));
11421164

11431165
Log.d(TAG, "Application Label :" + pm.getApplicationLabel(applicationInfo));
11441166

PowerFileExplorer/src/main/java/net/gnu/androidutil/BitmapUtil.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ public class BitmapUtil {
2020

2121
private static final String TAG = "BitmapUtil";
2222

23+
public static Bitmap drawableToBitmap(final Drawable drawable) {
24+
if (drawable instanceof BitmapDrawable ) {
25+
return (( BitmapDrawable ) drawable).getBitmap();
26+
}
27+
// We ask for the bounds if they have been set as they would be most
28+
// correct, then we check we are > 0
29+
final int width = !drawable.getBounds().isEmpty() ?
30+
drawable.getBounds().width() : drawable.getIntrinsicWidth();
31+
final int height = !drawable.getBounds().isEmpty() ?
32+
drawable.getBounds().height() : drawable.getIntrinsicHeight();
33+
// Now we check we are > 0
34+
final Bitmap bitmap = Bitmap.createBitmap(
35+
width <= 0 ? 1 : width,
36+
height <= 0 ? 1 : height,
37+
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
38+
final Canvas canvas = new Canvas(bitmap);
39+
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
40+
drawable.draw(canvas);
41+
return bitmap;
42+
}
43+
2344
public static Bitmap loadBitmapFromView(View v, final int w, final int h) {
2445
final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
2546
final Canvas c = new Canvas(b);
@@ -414,15 +435,6 @@ public static Bitmap drawableToBitmap(int res_id, Context context) {
414435
return bitmap;
415436
}
416437

417-
public static Bitmap drawableToBitmap(Drawable drawable) {
418-
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
419-
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
420-
Canvas canvas = new Canvas(bitmap);
421-
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
422-
drawable.draw(canvas);
423-
return bitmap;
424-
}
425-
426438
public static Drawable bitmapToDrawable(Bitmap bitmap) {
427439
return new BitmapDrawable(bitmap);
428440
}

PowerFileExplorer/src/main/java/net/gnu/explorer/AppsFragment.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.util.HashSet;
6262
import android.graphics.PorterDuff;
6363
import net.gnu.common.*;
64+
import android.content.pm.PackageManager.*;
6465

6566
public class AppsFragment extends FileFrag implements View.OnClickListener {
6667

@@ -1015,7 +1016,7 @@ public boolean onMenuItemSelected(MenuBuilder p1, MenuItem item) {
10151016
rowItem.version);//, AppsFragment.this);
10161017
return true;
10171018
case R.id.shortcut:
1018-
//AndroidUtils.createShortCut(AppsFragment.this.getContext(), rowItem.packageName, rowItem.label, image.setImageDrawable(packageManager.getApplicationIcon(appInfo.packageName)));
1019+
AndroidUtils.createShortCut(AppsFragment.this.getContext(), rowItem.packageName);
10191020
return true;
10201021
}
10211022
return true;
@@ -1093,16 +1094,18 @@ public boolean uninstall(String pkg) {
10931094
}
10941095

10951096
void backup(final String path, final String title, final String version) {
1097+
Log.d(TAG, "backup " + path + ", " + title + ", " + version);
10961098
//Toast.makeText(activity, activity.getResources().getString(R.string.copyingapk) + Environment.getExternalStorageDirectory().getPath() + "/app_backup", Toast.LENGTH_LONG).show();
10971099
File f = new File(path);
10981100
ArrayList<BaseFile> ab = new ArrayList<>();
10991101
File dst = new File(Environment.getExternalStorageDirectory().getPath() + "/app_backup");
1100-
if (!dst.exists() || !dst.isDirectory())dst.mkdirs();
1101-
Intent intent = new Intent(activity, CopyService.class);
1102+
if (!dst.exists() || !dst.isDirectory())
1103+
dst.mkdirs();
11021104
BaseFile baseFile=RootHelper.generateBaseFile(f, true);
11031105
baseFile.setName(title + "_" + version + ".apk");
11041106
ab.add(baseFile);
11051107

1108+
Intent intent = new Intent(activity, CopyService.class);
11061109
intent.putParcelableArrayListExtra(CopyService.TAG_COPY_SOURCES, ab);
11071110
intent.putExtra(CopyService.TAG_COPY_TARGET, dst.getPath());
11081111
intent.putExtra(CopyService.TAG_COPY_OPEN_MODE, 0);
@@ -1111,7 +1114,7 @@ void backup(final String path, final String title, final String version) {
11111114
}
11121115

11131116
public static void backup(final String path, final String title, final String version, FileFrag fileFrag) {
1114-
Log.d("AppsFragment", path + ", " + title + ", " + version);
1117+
Log.d(TAG, "backup: " + path + ", " + title + ", " + version);
11151118
File f = new File(path);
11161119
ArrayList<BaseFile> ab = new ArrayList<>();
11171120

PowerFileExplorer/src/main/java/net/gnu/explorer/DataTrackerFrag.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
912912
//arrayList.add(new File(rowItem.getPath()));
913913
Futils.shareFiles(arrayList2, activity, activity.getAppTheme(), accentColor);
914914
break;
915+
case R.id.shortcut:
916+
AndroidUtils.createShortCut(DataTrackerFrag.this.getContext(), pinfo.packageName);
917+
break;
915918
}
916919
return true;
917920
}

PowerFileExplorer/src/main/java/net/gnu/explorer/ProcessFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
13051305
Futils.shareFiles(arrayList2, activity, activity.getAppTheme(), accentColor);
13061306
break;
13071307
case R.id.shortcut:
1308-
//AndroidUtils.createShortCut(AppsFragment.this.getContext(), rowItem.packageName, rowItem.label, image.setImageDrawable(packageManager.getApplicationIcon(appInfo.packageName)));
1308+
AndroidUtils.createShortCut(ProcessFragment.this.getContext(), pinfo.packageName);
13091309
return true;
13101310
}
13111311
return true;

0 commit comments

Comments
 (0)