Example usage for android.app Activity getPackageManager

List of usage examples for android.app Activity getPackageManager

Introduction

In this page you can find the example usage for android.app Activity getPackageManager.

Prototype

@Override
    public PackageManager getPackageManager() 

Source Link

Usage

From source file:org.uguess.android.sysinfo.SiragonManager.java

/**
 * This checks the built-in app2sd storage info supported since Froyo
 *///from  w  w w  .  j  a v a  2 s.co  m
private String[] getSystemA2SDStorageInfo() {
    Activity ctx = getActivity();
    final PackageManager pm = ctx.getPackageManager();
    List<ApplicationInfo> allApps = pm.getInstalledApplications(0);

    long total = 0;
    long free = 0;

    for (int i = 0, size = allApps.size(); i < size; i++) {
        ApplicationInfo info = allApps.get(i);

        if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
            String src = info.sourceDir;

            if (src != null) {
                File srcFile = new File(src);

                if (srcFile.canRead()) {
                    try {
                        StatFs stat = new StatFs(srcFile.getAbsolutePath());
                        long blockSize = stat.getBlockSize();

                        total += stat.getBlockCount() * blockSize;
                        free += stat.getAvailableBlocks() * blockSize;
                    } catch (Exception e) {
                        Log.e(SiragonManager.class.getName(), "Cannot access path: " //$NON-NLS-1$
                                + srcFile.getAbsolutePath(), e);
                    }
                }
            }
        }
    }

    if (total > 0) {
        String[] info = new String[2];
        info[0] = Formatter.formatFileSize(ctx, total);
        info[1] = Formatter.formatFileSize(ctx, free);

        return info;
    }

    return null;
}

From source file:org.uguess.android.sysinfo.ProcessManager.java

void handleAction(final ProcessItem rap, int action) {
    Activity ctx = getActivity();

    switch (action) {
    case ACTION_END:

        if (!ignoreList.contains(rap.procInfo.processName) && !rap.sys) {
            ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

            String self = ctx.getPackageName();

            if (self.equals(rap.procInfo.processName)) {
                Util.killSelf(handler, ctx, am, self);
            } else {
                endProcess(am, rap.procInfo.pkgList, self);

                handler.removeCallbacks(task);
                handler.post(task);//from  w  w  w. j ava2  s .co  m
            }
        }

        break;
    case ACTION_END_OTHERS:

        endAllExcept(rap.procInfo.processName);

        break;
    case ACTION_SWITCH:

        String pkgName = rap.procInfo.processName;

        if (!pkgName.equals(ctx.getPackageName())) {
            Intent it = new Intent("android.intent.action.MAIN"); //$NON-NLS-1$
            it.addCategory(Intent.CATEGORY_LAUNCHER);

            List<ResolveInfo> acts = ctx.getPackageManager().queryIntentActivities(it, 0);

            if (acts != null) {
                boolean started = false;

                for (int i = 0, size = acts.size(); i < size; i++) {
                    ResolveInfo ri = acts.get(i);

                    if (pkgName.equals(ri.activityInfo.packageName)) {
                        it.setClassName(ri.activityInfo.packageName, ri.activityInfo.name);

                        it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                .addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);

                        try {
                            startActivity(it);

                            started = true;
                        } catch (Exception e) {
                            Log.e(ProcessManager.class.getName(), "Cannot start activity: " + pkgName, //$NON-NLS-1$
                                    e);
                        }

                        break;
                    }
                }

                if (!started) {
                    Util.shortToast(ctx, R.string.error_switch_task);
                }
            }
        }

        break;
    case ACTION_IGNORE:

        if (!ignoreList.contains(rap.procInfo.processName) && !rap.sys) {
            ignoreList.add(rap.procInfo.processName);

            setIgnoreList(ignoreList);

            if (IGNORE_ACTION_HIDDEN == Util.getIntOption(ctx, PSTORE_PROCESSMANAGER, PREF_KEY_IGNORE_ACTION,
                    IGNORE_ACTION_HIDDEN)) {
                handler.removeCallbacks(task);
                handler.post(task);
            }
        }

        break;
    case ACTION_DETAILS:

        String[] status = readProcStatus(rap.procInfo.pid);

        StringBuffer sb = new StringBuffer().append("<small>") //$NON-NLS-1$
                .append(getString(R.string.proc_name)).append(": ") //$NON-NLS-1$
                .append(rap.procInfo.processName).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.pid)).append(": ") //$NON-NLS-1$
                .append(rap.procInfo.pid).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.uid)).append(": ") //$NON-NLS-1$
                .append(status == null ? "" : status[1]) //$NON-NLS-1$
                .append("<br>") //$NON-NLS-1$
                .append(getString(R.string.gid)).append(": ") //$NON-NLS-1$
                .append(status == null ? "" : status[2]) //$NON-NLS-1$
                .append("<br>") //$NON-NLS-1$
                .append(getString(R.string.state)).append(": ") //$NON-NLS-1$
                .append(status == null ? "" : status[0]) //$NON-NLS-1$
                .append("<br>") //$NON-NLS-1$
                .append(getString(R.string.threads)).append(": ") //$NON-NLS-1$
                .append(status == null ? "" : status[3]) //$NON-NLS-1$
                .append("<br>") //$NON-NLS-1$
                .append(getString(R.string.importance)).append(": ") //$NON-NLS-1$
                .append(rap.procInfo.importance).append("<br>LRU: ") //$NON-NLS-1$
                .append(rap.procInfo.lru).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.pkg_name)).append(": "); //$NON-NLS-1$

        if (rap.procInfo.pkgList != null) {
            int i = 0;

            for (String pkg : rap.procInfo.pkgList) {
                if (pkg != null) {
                    if (i > 0) {
                        sb.append(", "); //$NON-NLS-1$
                    }
                    sb.append(pkg);
                    i++;
                }
            }
        }

        sb.append("</small>"); //$NON-NLS-1$

        new AlertDialog.Builder(ctx).setTitle(rap.label == null ? rap.procInfo.processName : rap.label)
                .setNeutralButton(R.string.close, null).setMessage(Html.fromHtml(sb.toString())).create()
                .show();

        break;
    case ACTION_MENU:

        final boolean protect = ignoreList.contains(rap.procInfo.processName) || rap.sys;

        OnClickListener listener = new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                // bypass the 'showMenu' action offset
                int action = which + 1;

                if (!protect || (action != ACTION_END && action != ACTION_IGNORE)) {
                    handleAction(rap, action);
                }
            }
        };

        new AlertDialog.Builder(
                ctx).setTitle(R.string.actions)
                        .setItems(
                                new CharSequence[] { getString(R.string.switch_to),
                                        protect ? Html.fromHtml("<font color=\"#848484\">" //$NON-NLS-1$
                                                + getString(R.string.end_task) + "</font>") //$NON-NLS-1$
                                                : getString(R.string.end_task),
                                        getString(R.string.end_others),
                                        protect ? Html.fromHtml("<font color=\"#848484\">" //$NON-NLS-1$
                                                + getString(R.string.ignore) + "</font>") //$NON-NLS-1$
                                                : getString(R.string.ignore),
                                        getString(R.string.details) },
                                listener)
                        .create().show();

        break;
    }
}

From source file:org.uguess.android.sysinfo.ApplicationManager.java

void handleAction(final AppInfoHolder ai, int action) {
    Activity ctx = getActivity();
    String pkgName = ai.appInfo.packageName;

    switch (action) {
    case ACTION_MENU:

        OnClickListener listener = new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();//from   ww  w  .ja v a  2  s . c o  m

                // bypass the 'showMenu' action offset
                int action = which + 1;

                handleAction(ai, action);
            }
        };

        new AlertDialog.Builder(ctx).setTitle(R.string.actions)
                .setItems(new CharSequence[] { getString(R.string.manage), getString(R.string.run),
                        getString(R.string.search_market), getString(R.string.details) }, listener)
                .create().show();

        break;
    case ACTION_MANAGE:

        Intent it = new Intent(Intent.ACTION_VIEW);

        it.setClassName("com.android.settings", //$NON-NLS-1$
                "com.android.settings.InstalledAppDetails"); //$NON-NLS-1$
        it.putExtra("com.android.settings.ApplicationPkgName", pkgName); //$NON-NLS-1$
        // this is for Froyo
        it.putExtra("pkg", pkgName); //$NON-NLS-1$

        List<ResolveInfo> acts = ctx.getPackageManager().queryIntentActivities(it, 0);

        if (acts.size() > 0) {
            startActivity(it);
        } else {
            // for ginger bread
            it = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", //$NON-NLS-1$
                    Uri.fromParts("package", pkgName, null)); //$NON-NLS-1$

            acts = ctx.getPackageManager().queryIntentActivities(it, 0);

            if (acts.size() > 0) {
                startActivity(it);
            } else {
                Log.d(ApplicationManager.class.getName(), "Failed to resolve activity for InstalledAppDetails"); //$NON-NLS-1$
            }
        }

        break;
    case ACTION_LAUNCH:

        if (!pkgName.equals(ctx.getPackageName())) {
            it = new Intent("android.intent.action.MAIN"); //$NON-NLS-1$
            it.addCategory(Intent.CATEGORY_LAUNCHER);

            acts = ctx.getPackageManager().queryIntentActivities(it, 0);

            if (acts != null) {
                boolean started = false;

                for (int i = 0, size = acts.size(); i < size; i++) {
                    ResolveInfo ri = acts.get(i);

                    if (pkgName.equals(ri.activityInfo.packageName)) {
                        it.setClassName(ri.activityInfo.packageName, ri.activityInfo.name);

                        it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        try {
                            startActivity(it);

                            started = true;

                        } catch (Exception e) {
                            Log.e(ApplicationManager.class.getName(), "Cannot start activity: " + pkgName, //$NON-NLS-1$
                                    e);
                        }

                        break;
                    }
                }

                if (!started) {
                    Util.shortToast(ctx, R.string.run_failed);
                }
            }
        }

        break;
    case ACTION_SEARCH:

        it = new Intent(Intent.ACTION_VIEW);

        it.setData(Uri.parse("market://search?q=pname:" + pkgName)); //$NON-NLS-1$

        it = Intent.createChooser(it, null);

        startActivity(it);

        break;
    case ACTION_DETAILS:

        ApplicationInfo appInfo = ai.appInfo;

        String installDate;
        String fileSize;

        if (appInfo.sourceDir != null) {
            File f = new File(appInfo.sourceDir);
            installDate = DateUtils.formatDateTime(ctx, f.lastModified(),
                    DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
            fileSize = Formatter.formatFileSize(ctx, f.length());
        } else {
            installDate = fileSize = getString(R.string.unknown);
        }

        StringBuffer sb = new StringBuffer().append("<small>") //$NON-NLS-1$
                .append(getString(R.string.pkg_name)).append(": ") //$NON-NLS-1$
                .append(appInfo.packageName).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.version_code)).append(": ") //$NON-NLS-1$
                .append(ai.versionCode).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.target_sdk)).append(": ") //$NON-NLS-1$
                .append(Util.getTargetSdkVersion(ctx, appInfo)).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.uid)).append(": ") //$NON-NLS-1$
                .append(appInfo.uid).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.file_size)).append(": ") //$NON-NLS-1$
                .append(fileSize).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.public_source)).append(": ") //$NON-NLS-1$
                .append(appInfo.publicSourceDir).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.source)).append(": ") //$NON-NLS-1$
                .append(appInfo.sourceDir).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.data)).append(": ") //$NON-NLS-1$
                .append(appInfo.dataDir).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.installed_date)).append(": ") //$NON-NLS-1$
                .append(installDate).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.process)).append(": ") //$NON-NLS-1$
                .append(appInfo.processName).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.app_class)).append(": ") //$NON-NLS-1$
                .append(appInfo.className == null ? "" //$NON-NLS-1$
                        : appInfo.className)
                .append("<br>") //$NON-NLS-1$
                .append(getString(R.string.task_affinity)).append(": ") //$NON-NLS-1$
                .append(appInfo.taskAffinity).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.permission)).append(": ") //$NON-NLS-1$
                .append(appInfo.permission == null ? "" //$NON-NLS-1$
                        : appInfo.permission)
                .append("<br>") //$NON-NLS-1$
                .append(getString(R.string.flags)).append(": ") //$NON-NLS-1$
                .append(appInfo.flags).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.enabled)).append(": ") //$NON-NLS-1$
                .append(appInfo.enabled).append("<br>") //$NON-NLS-1$
                .append(getString(R.string.manage_space_ac)).append(": ") //$NON-NLS-1$
                .append(appInfo.manageSpaceActivityName == null ? "" //$NON-NLS-1$
                        : appInfo.manageSpaceActivityName)
                .append("</small>"); //$NON-NLS-1$

        new AlertDialog.Builder(ctx).setTitle(ai.label == null ? appInfo.packageName : ai.label)
                .setNeutralButton(R.string.close, null).setMessage(Html.fromHtml(sb.toString())).create()
                .show();

        break;
    }
}

From source file:de.baumann.hhsmoodle.helper.helper_webView.java

public static void webView_WebViewClient(final Activity from, final SwipeRefreshLayout swipeRefreshLayout,
        final WebView webView) {

    webView.setWebViewClient(new WebViewClient() {

        ProgressDialog progressDialog;/*from ww w.j a  v a2  s.  co m*/
        int numb;

        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            ViewPager viewPager = (ViewPager) from.findViewById(R.id.viewpager);
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(from);
            sharedPref.edit().putString("loadURL", webView.getUrl()).apply();

            if (viewPager.getCurrentItem() == 0) {
                if (url != null) {
                    from.setTitle(webView.getTitle());
                }
            }

            if (url != null && url.contains("moodle.huebsch.ka.schule-bw.de/moodle/")
                    && url.contains("/login/")) {

                if (viewPager.getCurrentItem() == 0 && numb != 1) {
                    progressDialog = new ProgressDialog(from);
                    progressDialog.setTitle(from.getString(R.string.login_title));
                    progressDialog.setMessage(from.getString(R.string.login_text));
                    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                            from.getString(R.string.toast_settings), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    helper_main.switchToActivity(from, Activity_settings.class, true);
                                }
                            });
                    progressDialog.show();
                    numb = 1;
                    progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            numb = 0;
                        }
                    });
                }

            } else if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.cancel();
                numb = 0;
            }

            swipeRefreshLayout.setRefreshing(false);

            class_SecurePreferences sharedPrefSec = new class_SecurePreferences(from, "sharedPrefSec",
                    "Ywn-YM.XK$b:/:&CsL8;=L,y4", true);
            String username = sharedPrefSec.getString("username");
            String password = sharedPrefSec.getString("password");

            final String js = "javascript:" + "document.getElementById('password').value = '" + password + "';"
                    + "document.getElementById('username').value = '" + username + "';"
                    + "var ans = document.getElementsByName('answer');"
                    + "document.getElementById('loginbtn').click()";

            if (Build.VERSION.SDK_INT >= 19) {
                view.evaluateJavascript(js, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
            } else {
                view.loadUrl(js);
            }
        }

        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            final Uri uri = Uri.parse(url);
            return handleUri(uri);
        }

        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            final Uri uri = request.getUrl();
            return handleUri(uri);
        }

        private boolean handleUri(final Uri uri) {
            Log.i(TAG, "Uri =" + uri);
            final String url = uri.toString();
            // Based on some condition you need to determine if you are going to load the url
            // in your web view itself or in a browser.
            // You can use `host` or `scheme` or any part of the `uri` to decide.

            if (url.startsWith("http"))
                return false;//open web links as usual
            //try to find browse activity to handle uri
            Uri parsedUri = Uri.parse(url);
            PackageManager packageManager = from.getPackageManager();
            Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
            if (browseIntent.resolveActivity(packageManager) != null) {
                from.startActivity(browseIntent);
                return true;
            }
            //if not activity found, try to parse intent://
            if (url.startsWith("intent:")) {
                try {
                    Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                    if (intent.resolveActivity(from.getPackageManager()) != null) {
                        from.startActivity(intent);
                        return true;
                    }
                    //try to find fallback url
                    String fallbackUrl = intent.getStringExtra("browser_fallback_url");
                    if (fallbackUrl != null) {
                        webView.loadUrl(fallbackUrl);
                        return true;
                    }
                    //invite to install
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW)
                            .setData(Uri.parse("market://details?id=" + intent.getPackage()));
                    if (marketIntent.resolveActivity(packageManager) != null) {
                        from.startActivity(marketIntent);
                        return true;
                    }
                } catch (URISyntaxException e) {
                    //not an intent uri
                }
            }
            return true;//do nothing in other cases
        }
    });
}

From source file:org.uguess.android.sysinfo.ApplicationManager.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Activity ctx = getActivity();

    if (item.getItemId() == MI_PREFERENCE) {
        Intent it = new Intent(ctx, AppSettings.class);

        it.putExtra(PREF_KEY_FILTER_APP_TYPE,
                Util.getIntOption(ctx, PSTORE_APPMANAGER, PREF_KEY_FILTER_APP_TYPE, APP_TYPE_ALL));
        it.putExtra(PREF_KEY_APP_EXPORT_DIR,
                Util.getStringOption(ctx, PSTORE_APPMANAGER, PREF_KEY_APP_EXPORT_DIR, DEFAULT_EXPORT_FOLDER));
        it.putExtra(PREF_KEY_SORT_ORDER_TYPE,
                Util.getIntOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SORT_ORDER_TYPE, ORDER_TYPE_NAME));
        it.putExtra(PREF_KEY_SORT_DIRECTION,
                Util.getIntOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SORT_DIRECTION, ORDER_ASC));
        it.putExtra(PREF_KEY_SHOW_SIZE, Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_SIZE));
        it.putExtra(PREF_KEY_SHOW_DATE, Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_DATE));
        it.putExtra(PREF_KEY_SHOW_ICON, Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_ICON));
        it.putExtra(PREF_KEY_SHOW_BACKUP_STATE,
                Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_BACKUP_STATE));
        it.putExtra(PREF_KEY_DEFAULT_TAP_ACTION,
                Util.getIntOption(ctx, PSTORE_APPMANAGER, PREF_KEY_DEFAULT_TAP_ACTION, ACTION_MENU));

        startActivityForResult(it, REQUEST_SETTINGS);

        return true;
    } else if (item.getItemId() == MI_REVERT) {
        Intent it = new Intent(ctx, RestoreAppActivity.class);

        it.putExtra(KEY_RESTORE_PATH, new File(
                Util.getStringOption(ctx, PSTORE_APPMANAGER, PREF_KEY_APP_EXPORT_DIR, DEFAULT_EXPORT_FOLDER),
                USER_APP).getAbsolutePath());
        it.putExtra(KEY_ARCHIVE_PATH, new File(
                Util.getStringOption(ctx, PSTORE_APPMANAGER, PREF_KEY_APP_EXPORT_DIR, DEFAULT_EXPORT_FOLDER),
                ARCHIVED).getAbsolutePath());

        startActivityForResult(it, REQUEST_RESTORE);

        return true;
    } else if (item.getItemId() == MI_USAGE_STATS) {
        Intent it = Util.getSettingsIntent(ctx.getPackageManager(), "com.android.settings.UsageStats"); //$NON-NLS-1$

        if (it != null) {
            startActivity(it);// w w w.j  a  va2s  . c  o  m
        }

        return true;
    } else if (item.getItemId() == MI_SHARE) {
        doShare();

        return true;
    } else if (item.getItemId() == MI_DELETE) {
        doUninstall();

        return true;
    }

    return false;
}

From source file:org.uguess.android.sysinfo.SiragonManager.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final Activity ctx = getActivity();

    if (item.getItemId() == MI_PREFERENCE) {
        Intent it = new Intent(ctx, InfoSettings.class);

        it.putExtra(PREF_KEY_SHOW_INFO_ICON,
                Util.getBooleanOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_SHOW_INFO_ICON));
        it.putExtra(PREF_KEY_SHOW_TASK_ICON,
                Util.getBooleanOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_SHOW_TASK_ICON));
        it.putExtra(PREF_KEY_AUTO_START_ICON,
                Util.getBooleanOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_AUTO_START_ICON, false));
        it.putExtra(PREF_KEY_DEFAULT_SERVER,
                Util.getStringOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_DEFAULT_SERVER, null));
        it.putExtra(PREF_KEY_DEFAULT_EMAIL,
                Util.getStringOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_DEFAULT_EMAIL, null));
        it.putExtra(PREF_KEY_DEFAULT_TAB,
                Util.getIntOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_DEFAULT_TAB, 0));
        it.putExtra(PREF_KEY_WIDGET_DISABLED,
                Util.getStringOption(ctx, PSTORE_SYSINFOMANAGER, PREF_KEY_WIDGET_DISABLED, null));

        startActivityForResult(it, 2);//from ww  w.  j av a2  s.c o m

        return true;
    } else if (item.getItemId() == MI_HELP) {
        Intent it = new Intent(Intent.ACTION_VIEW);

        String target = "http://www.siragon.com.ve"; //$NON-NLS-1$

        ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (info != null && info.isConnected()) {
            target = "http://www.siragon.com.ve"; //$NON-NLS-1$
        }

        it.setData(Uri.parse(target));

        it = Intent.createChooser(it, null);

        startActivity(it);

        return true;
    }
    ///////////////////////PATH SERVER///////////////////////////////////

    else if (item.getItemId() == MI_ABOUT) {
        ScrollView sv = new ScrollView(ctx);

        TextView txt = new TextView(ctx);
        txt.setGravity(Gravity.CENTER_HORIZONTAL);
        txt.setTextAppearance(ctx, android.R.style.TextAppearance_Medium);

        sv.addView(txt);

        String href = "http://www.google.com.ve"; //$NON-NLS-1$

        txt.setText(Html.fromHtml(getString(R.string.about_msg,
                getVersionName(ctx.getPackageManager(), ctx.getPackageName()), href)));
        txt.setMovementMethod(LinkMovementMethod.getInstance());

        new AlertDialog.Builder(ctx).setTitle(R.string.app_name).setIcon(R.drawable.logo2).setView(sv)
                .setNegativeButton(R.string.close, null).create().show();

        return true;
    } else if (item.getItemId() == MI_EXIT) {
        OnClickListener listener = new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Util.killSelf(handler, ctx, (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE),
                        ctx.getPackageName());
            }
        };

        new AlertDialog.Builder(ctx).setTitle(R.string.prompt).setMessage(R.string.exit_prompt)
                .setPositiveButton(android.R.string.yes, listener).setNegativeButton(android.R.string.no, null)
                .create().show();

        return true;
    } else if (item.getItemId() == MI_REFRESH) {
        updateInfo();
        return true;
    }

    return false;
}

From source file:org.uguess.android.sysinfo.ApplicationManager.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.app_lst_view, container, false);

    ((Button) rootView.findViewById(R.id.btn_export)).setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            doExport();/*  w ww  . ja  va  2s  . co  m*/
        }
    });

    ((Button) rootView.findViewById(R.id.btn_sel_all)).setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            toggleAllSelection(true);
        }
    });

    ((Button) rootView.findViewById(R.id.btn_desel_all)).setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            toggleAllSelection(false);
        }
    });

    ListView lstApps = (ListView) rootView.findViewById(android.R.id.list);

    lstApps.setFastScrollEnabled(true);

    registerForContextMenu(lstApps);

    ArrayAdapter<AppInfoHolder> adapter = new ArrayAdapter<AppInfoHolder>(getActivity(), R.layout.app_item) {

        public android.view.View getView(int position, android.view.View convertView,
                android.view.ViewGroup parent) {
            View view;
            TextView txt_name, txt_size, txt_ver, txt_time;
            ImageView img_type;
            CheckBox ckb_app;

            Activity ctx = getActivity();

            if (convertView == null) {
                view = ctx.getLayoutInflater().inflate(R.layout.app_item, parent, false);
            } else {
                view = convertView;
            }

            if (position >= getCount()) {
                return view;
            }

            AppInfoHolder itm = getItem(position);

            txt_name = (TextView) view.findViewById(R.id.app_name);
            if (itm.label != null) {
                txt_name.setText(itm.label);
            } else {
                txt_name.setText(itm.appInfo.packageName);
            }

            if (Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_BACKUP_STATE)) {
                switch (itm.backupState) {
                case 1:
                    txt_name.setTextColor(Color.YELLOW);
                    break;
                case 2:
                    txt_name.setTextColor(0xff00bb00);
                    break;
                case 3:
                    txt_name.setTextColor(0xffF183BD);
                    break;
                default:
                    txt_name.setTextColor(Color.WHITE);
                    break;
                }
            } else {
                txt_name.setTextColor(Color.WHITE);
            }

            txt_ver = (TextView) view.findViewById(R.id.app_version);
            txt_ver.setText(itm.version);

            txt_size = (TextView) view.findViewById(R.id.app_size);
            if (Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_SIZE)) {
                txt_size.setVisibility(View.VISIBLE);

                if (itm.size != null) {
                    txt_size.setText(itm.size);
                } else {
                    txt_size.setText(R.string.computing);
                }
            } else {
                txt_size.setVisibility(View.GONE);
            }

            txt_time = (TextView) view.findViewById(R.id.app_time);
            if (Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_DATE)) {
                txt_time.setVisibility(View.VISIBLE);

                if (itm.appInfo.sourceDir != null) {
                    File f = new File(itm.appInfo.sourceDir);
                    txt_time.setText(DateUtils.formatDateTime(ctx, f.lastModified(), DateUtils.FORMAT_SHOW_YEAR
                            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
                } else {
                    txt_time.setText(R.string.unknown);
                }
            } else {
                txt_time.setVisibility(View.GONE);
            }

            img_type = (ImageView) view.findViewById(R.id.img_app_icon);
            if (Util.getBooleanOption(ctx, PSTORE_APPMANAGER, PREF_KEY_SHOW_ICON)) {
                img_type.setVisibility(View.VISIBLE);

                if (itm.icon != null) {
                    img_type.setImageDrawable(itm.icon);
                } else {
                    try {
                        img_type.setImageDrawable(ctx.getPackageManager().getDefaultActivityIcon());
                    } catch (Exception fe) {
                        img_type.setImageDrawable(null);

                        Log.e(ApplicationManager.class.getName(), fe.getLocalizedMessage());
                    }
                }
            } else {
                img_type.setVisibility(View.GONE);
            }

            ckb_app = (CheckBox) view.findViewById(R.id.ckb_app);
            ckb_app.setTag(position);
            ckb_app.setChecked(itm.checked);
            ckb_app.setOnCheckedChangeListener(checkListener);

            View imgLock = view.findViewById(R.id.img_lock);
            imgLock.setVisibility(itm.isPrivate ? View.VISIBLE : View.GONE);

            return view;
        }
    };

    setListAdapter(adapter);

    return rootView;
}