Android Open Source - FAST App Action Dialog Builder






From Project

Back to project page FAST.

License

The source code is released under:

GNU General Public License

If you think the Android project FAST listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.ligi.fast.ui;
//from w  ww .j  a  va 2s  .  c  o m
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import org.ligi.fast.App;
import org.ligi.fast.model.AppInfo;
import org.ligi.fast.R;
import org.ligi.fast.TargetStore;
import org.ligi.tracedroid.logging.Log;

import java.util.ArrayList;

public class AppActionDialogBuilder extends AlertDialog.Builder {
    private static final String SCHEME = "package";
    private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
    private static final String APP_PKG_NAME_22 = "pkg";
    private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
    private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
    private final AppInfo app_info;
    private final Context context;

    public AppActionDialogBuilder(Context _context, AppInfo _app_info) {

        super(_context);
        app_info = _app_info;
        context = _context;

        ArrayList<LabelAndCode> fkt_map = new ArrayList<LabelAndCode>();

        fkt_map.add(new LabelAndCode(context.getString(R.string.application_details), new Runnable() {
            @Override
            public void run() {
                showInstalledAppDetails(context, app_info.getPackageName());
            }
        }));

        fkt_map.add(new LabelAndCode(context.getString(R.string.open_as_notification), new OpenAsNotificationRunnable()));

        if (App.getSettings().isMarketForAllActivated() || isMarketApp()) {
            fkt_map.add(new LabelAndCode(context.getString(R.string.open_in) + " " + TargetStore.STORE_NAME, new Runnable() {
                @Override
                public void run() {
                    try {
                        context.startActivity(new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse(TargetStore.STORE_URL + app_info.getPackageName())));
                    } catch (android.content.ActivityNotFoundException anfe) {

                    }
                }
            }));
        }

        fkt_map.add(new LabelAndCode(context.getString(R.string.share), new Runnable() {
            @Override
            public void run() {
                try {
                    String message = "check out this app: " + App.getStoreURL4PackageName(app_info.getPackageName());
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("text/plain");
                    share.putExtra(Intent.EXTRA_TEXT, message);

                    context.startActivity(Intent.createChooser(share, "Share FAST"));
                } catch (android.content.ActivityNotFoundException anfe) {

                }
            }
        }));

        if (hasShortCutPermission()) {
            fkt_map.add(new LabelAndCode(context.getString(R.string.create_shortcut), new CreateShortCutRunnable()));
        }

        CharSequence[] items = new CharSequence[fkt_map.size()];
        final Runnable[] item_code = new Runnable[fkt_map.size()];

        for (int i = 0; i < fkt_map.size(); i++) {
            items[i] = fkt_map.get(i).label;
            item_code[i] = fkt_map.get(i).code;
        }


        setItems(items, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                item_code[which].run();
            }
        });

    }

    //  @TargetApi(5)
    // TODO find out why the above is not working and I need to use SupressLint to get rid of the error
    @SuppressLint("newApi")
    private static void showInstalledAppDetails(Context context,

                                                String packageName) {
        Intent intent = new Intent();
        final int apiLevel = Build.VERSION.SDK_INT;
        if (apiLevel >= 9) { // above 2.3
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts(SCHEME, packageName, null);
            intent.setData(uri);
        } else { // below 2.3
            final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
                    : APP_PKG_NAME_21);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName(APP_DETAILS_PACKAGE_NAME,
                    APP_DETAILS_CLASS_NAME);
            intent.putExtra(appPkgName, packageName);
        }
        context.startActivity(intent);
    }

    private boolean hasShortCutPermission() {
        try {
            String permission = "com.android.launcher.permission.INSTALL_SHORTCUT";
            int res = context.checkCallingOrSelfPermission(permission);
            return (res == PackageManager.PERMISSION_GRANTED);
        } catch (Exception e) {
            return false;
        }
    }

    private boolean isMarketApp() {
        try {
            if (app_info.getPackageName() == null)
                return false;
            PackageManager packageManager = context.getPackageManager();

            if (packageManager == null) {
                Log.w("strange - there was no PackageManager - might lie to the user now with false"
                        + "as I cannot determine the correct answer to the question isMarketApp()");
                return false;
            }

            String installer_pkg = packageManager.getInstallerPackageName(app_info.getPackageName());
            return installer_pkg != null && installer_pkg.startsWith(TargetStore.STORE_PNAME);
        } catch (Exception e) {
            return false;
        }
    }

    private class OpenAsNotificationRunnable implements Runnable {
        @Override
        public void run() {
            final Intent notifyIntent = app_info.getIntent();

            PendingIntent intent = PendingIntent.getActivity(context, 0, notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

            final String title=app_info.getLabel();
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher, title, System.currentTimeMillis());
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.setLatestEventInfo(context,title,context.getString(R.string.appActionDialog_title), intent);
            notificationManager.notify((int) (Math.random() * Integer.MAX_VALUE), notification);
        }
    }

    private class LabelAndCode {
        public String label;
        public Runnable code;

        public LabelAndCode(String label, Runnable code) {
            this.code = code;
            this.label = label;
        }

    }

    private class CreateShortCutRunnable implements Runnable {
        @Override
        public void run() {
            Intent shortcutIntent = new Intent();
            shortcutIntent.setClassName(app_info.getPackageName(), app_info.getLabel());
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
            Intent create_shortcut_intent = new Intent();
            create_shortcut_intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            // Sets the custom shortcut's title
            create_shortcut_intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, app_info.getLabel());

            BitmapDrawable bd = (BitmapDrawable) (app_info.getIcon());
            Bitmap newBitmap;
            newBitmap = bd.getBitmap();
            create_shortcut_intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newBitmap);

            create_shortcut_intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            context.sendBroadcast(create_shortcut_intent);
        }
    }
}




Java Source Code List

org.ligi.axt.helpers.ActivityHelper.java
org.ligi.axt.helpers.ContextHelper.java
org.ligi.axt.helpers.FileHelper.java
org.ligi.axt.helpers.ResolveInfoHelper.java
org.ligi.axt.helpers.ViewHelper.java
org.ligi.axt.simplifications.SimpleTextWatcher.java
org.ligi.fast.App.java
org.ligi.fast.TargetStore.java
org.ligi.fast.TargetStore.java
org.ligi.fast.TargetStore.java
org.ligi.fast.background.AppInstallOrRemoveReceiver.java
org.ligi.fast.background.BackgroundGatherAsyncTask.java
org.ligi.fast.background.BaseAppGatherAsyncTask.java
org.ligi.fast.model.AppIconCache.java
org.ligi.fast.model.AppInfoList.java
org.ligi.fast.model.AppInfoSortByLabelComparator.java
org.ligi.fast.model.AppInfoSortByMostUsedComparator.java
org.ligi.fast.model.AppInfo.java
org.ligi.fast.model.DynamicAppInfoList.java
org.ligi.fast.settings.AndroidFASTSettings.java
org.ligi.fast.settings.FASTSettings.java
org.ligi.fast.testing.AppInfoTestBase.java
org.ligi.fast.testing.MutableFastSettings.java
org.ligi.fast.testing.TheAppIconCache.java
org.ligi.fast.testing.TheAppInfoStore.java
org.ligi.fast.testing.TheAppInfo.java
org.ligi.fast.testing.TheSearchActivity.java
org.ligi.fast.ui.AppActionDialogBuilder.java
org.ligi.fast.ui.AppInfoAdapter.java
org.ligi.fast.ui.FASTSettingsActivity.java
org.ligi.fast.ui.HelpDialog.java
org.ligi.fast.ui.HelpDialog.java
org.ligi.fast.ui.IconDimensions.java
org.ligi.fast.ui.LoadingDialog.java
org.ligi.fast.ui.SearchActivity.java
org.ligi.fast.util.AppInfoListStore.java
org.ligi.fast.util.PackageListSerializer.java
org.ligi.fast.util.StringUtils.java
org.ligi.fast.util.UmlautConverter.java