4646import java .security .SecureRandom ;
4747import javax .crypto .SecretKey ;
4848import net .gnu .common .*;
49+ import android .content .Intent .*;
4950
5051
5152public 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 ("\n Certificate version: " ).append (x509Cert .getVersion ());
428450 sb .append ("\n Certificate not before: " ).append (x509Cert .getNotBefore ());
429451 sb .append ("\n Certificate 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
0 commit comments