Example usage for android.content.pm PackageManager getApplicationInfo

List of usage examples for android.content.pm PackageManager getApplicationInfo

Introduction

In this page you can find the example usage for android.content.pm PackageManager getApplicationInfo.

Prototype

public abstract ApplicationInfo getApplicationInfo(String packageName, @ApplicationInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve all of the information we know about a particular package/application.

Usage

From source file:mobisocial.bento.anyshare.ui.FeedItemListActivity.java

private boolean isFileManagerInstalled() {
    PackageManager pm = this.getPackageManager();
    String[] packages = { "com.rhmsoft.fm", "com.metago.astro", "com.estrongs.android.pop",
            "com.agilesoftresource", "com.agilesoftresource", "com.smartwho.SmartFileManager" };
    for (int i = 0; i < packages.length; i++) {
        try {/*ww w .j  ava 2 s .c om*/
            pm.getApplicationInfo(packages[i], 0);
            return true;
        } catch (NameNotFoundException e) {
            // check next
        }
    }
    return false;
}

From source file:org.wso2.iot.agent.adapters.AppDrawerAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (appList != null) {
        View view;//  w  ww . j  av a  2s . c o  m
        if (convertView == null) {
            // inflating a new view only when necessary
            view = inflater.inflate(R.layout.kiosk_app_drawer, null);
        } else {
            view = convertView;
        }

        Holder holder = new Holder();
        holder.appName = (TextView) view.findViewById(R.id.name);
        holder.appIcon = (ImageView) view.findViewById(R.id.icon);
        PackageManager pm = context.getPackageManager();

        String packageName = appList.get(position);
        if (packageName.equals(NEW_APP)) {
            holder.appName.setText(NEW_APP);
            holder.appIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_download));
        } else {
            ApplicationInfo applicationInfo = null;
            try {
                applicationInfo = pm.getApplicationInfo(packageName, 0);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Package name not found", e);
            }
            holder.appName.setText(pm.getApplicationLabel(applicationInfo).toString());
            try {
                holder.appIcon.setImageDrawable(pm.getApplicationIcon(packageName));
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Package name not found", e);
            }
        }
        return view;
    } else {
        return null;
    }
}

From source file:com.cleanwiz.applock.ui.activity.AppLockActivity.java

private void checkLockInfos() {
    PackageManager pkgMgr = getPackageManager();
    for (int i = lockInfos.size() - 1; i >= 0; i--) {
        CommLockInfo lock = lockInfos.get(i);
        ApplicationInfo appInfo = null;/*from  w ww .  j a  va2 s . co m*/
        try {
            appInfo = pkgMgr.getApplicationInfo(lock.getPackageName(), PackageManager.GET_UNINSTALLED_PACKAGES);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            lockInfos.remove(lock);
            continue;
        }
        if (appInfo == null || pkgMgr.getApplicationIcon(appInfo) == null) {
            lockInfos.remove(lock);
            continue;
        }
    }

}

From source file:com.kuloud.android.aboutlibraries.ui.LibsFragment.java

private void generateAboutThisAppSection() {
    if (aboutShowIcon != null
            && (aboutShowVersion != null || aboutShowVersionName != null || aboutShowVersionCode)) {
        //get the packageManager to load and read some values :D
        PackageManager pm = getActivity().getPackageManager();
        //get the packageName
        String packageName = getActivity().getPackageName();
        //Try to load the applicationInfo
        ApplicationInfo appInfo = null;//  w  ww  . j  a v a2s . c  o  m
        PackageInfo packageInfo = null;
        try {
            appInfo = pm.getApplicationInfo(packageName, 0);
            packageInfo = pm.getPackageInfo(packageName, 0);
        } catch (Exception ex) {
        }

        //Set the Icon or hide it
        Drawable icon = null;
        if (aboutShowIcon && appInfo != null) {
            icon = appInfo.loadIcon(pm);
        }

        //set the Version or hide it
        String versionName = null;
        Integer versionCode = null;
        if (packageInfo != null) {
            versionName = packageInfo.versionName;
            versionCode = packageInfo.versionCode;
        }

        //add this cool thing to the headerView of our listView
        mAdapter.setHeader(aboutAppName, aboutDescription, aboutSpecial1, aboutSpecial1Description,
                aboutSpecial2, aboutSpecial2Description, aboutSpecial3, aboutSpecial3Description, versionName,
                versionCode, aboutShowVersion, aboutShowVersionName, aboutShowVersionCode, icon, aboutShowIcon);
    }
}

From source file:com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushIntentService.java

private String getNotificationTitle(Context context) {
    // Check if push_notification_title is defined, if not get the
    // application name
    int notificationTitle = -1;
    try {//from   ww w  .j a  va 2  s  .  c o m
        notificationTitle = MFPPushUtils.getResourceId(getApplicationContext(), "string",
                "push_notification_title");
        return context.getString(notificationTitle);
    } catch (Exception e) {
        // ignore the exception
    }

    if (notificationTitle == -1) {
        ApplicationInfo appInfo = null;
        PackageManager packManager = context.getPackageManager();
        try {
            appInfo = packManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (Exception e) {
            logger.warn(
                    "MFPPushIntentService:getNotificationTitle() - Notification will not have a title because application name is not available.");
        }

        if (appInfo != null) {
            return (String) packManager.getApplicationLabel(appInfo);
        }
    }

    return "";
}

From source file:com.samsung.multiwindow.MultiWindow.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {

    if (Log.isLoggable(MULTIWINDOW, Log.DEBUG)) {
        Log.d(TAG, "Initialize multiwindow");
    }/*from ww w. ja v  a2  s.co m*/
    super.initialize(cordova, webView);
    String packageName = cordova.getActivity().getPackageName();
    PackageManager pm = cordova.getActivity().getPackageManager();
    ApplicationInfo ai = null;
    try {
        ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        if (ai.metaData != null) {
            pluginMetadata = ai.metaData.getBoolean(META_DATA);
        }
    } catch (NameNotFoundException e) {
        Log.e("MultiWindow", e.getMessage());
    }
}

From source file:me.piebridge.prevent.ui.UserGuideActivity.java

private boolean setView(int id, String packageName) {
    TextView donate = (TextView) findViewById(id);
    PackageManager pm = getPackageManager();
    try {// w  ww.  j ava  2 s  .co  m
        ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
        if (!info.enabled) {
            return false;
        }
        CharSequence label = getLabel(pm, info);
        donate.setContentDescription(label);
        donate.setCompoundDrawablesWithIntrinsicBounds(null, cropDrawable(pm.getApplicationIcon(info)), null,
                null);
        donate.setText(label);
        donate.setClickable(true);
        donate.setOnClickListener(this);
        donate.setVisibility(View.VISIBLE);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        UILog.d("cannot find package " + packageName, e);
        return false;
    }
}

From source file:com.tomer.alwayson.SettingsFragment.java

private boolean hasUsageAccess() throws PackageManager.NameNotFoundException {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
                applicationInfo.packageName);
        return mode == AppOpsManager.MODE_ALLOWED;
    } else/*from w  w  w . j a  v  a2  s  .co m*/
        return true;
}

From source file:com.google.android.apps.iosched.ui.SessionDetailActivity.java

/** Handle Moderator link. */
public void onModeratorClick(View v) {
    boolean appPresent = false;
    try {/*from  w ww .  j ava 2  s  .c om*/
        final PackageManager pm = getPackageManager();
        final ApplicationInfo ai = pm.getApplicationInfo(MODERATOR_PACKAGE, 0);
        if (ai != null)
            appPresent = true;
    } catch (NameNotFoundException e) {
        Log.w(TAG, "Problem searching for moderator: " + e.toString());
    }

    if (appPresent) {
        // Directly launch intent when already installed
        startModerator();
    } else {
        // Otherwise suggest installing app from Market
        showDialog(R.id.dialog_moderator);
    }
}

From source file:android_network.hetnet.vpn_service.AdapterLog.java

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
    // Get values
    final long id = cursor.getLong(colID);
    long time = cursor.getLong(colTime);
    int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion));
    int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol));
    String flags = cursor.getString(colFlags);
    String saddr = cursor.getString(colSAddr);
    int sport = (cursor.isNull(colSPort) ? -1 : cursor.getInt(colSPort));
    String daddr = cursor.getString(colDAddr);
    int dport = (cursor.isNull(colDPort) ? -1 : cursor.getInt(colDPort));
    String dname = (cursor.isNull(colDName) ? null : cursor.getString(colDName));
    int uid = (cursor.isNull(colUid) ? -1 : cursor.getInt(colUid));
    String data = cursor.getString(colData);
    int allowed = (cursor.isNull(colAllowed) ? -1 : cursor.getInt(colAllowed));
    int connection = (cursor.isNull(colConnection) ? -1 : cursor.getInt(colConnection));
    int interactive = (cursor.isNull(colInteractive) ? -1 : cursor.getInt(colInteractive));

    // Get views//from  w w w.  j a  va 2s  . c o m
    TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
    TextView tvProtocol = (TextView) view.findViewById(R.id.tvProtocol);
    TextView tvFlags = (TextView) view.findViewById(R.id.tvFlags);
    TextView tvSAddr = (TextView) view.findViewById(R.id.tvSAddr);
    TextView tvSPort = (TextView) view.findViewById(R.id.tvSPort);
    final TextView tvDaddr = (TextView) view.findViewById(R.id.tvDAddr);
    TextView tvDPort = (TextView) view.findViewById(R.id.tvDPort);
    final TextView tvOrganization = (TextView) view.findViewById(R.id.tvOrganization);
    ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
    TextView tvUid = (TextView) view.findViewById(R.id.tvUid);
    TextView tvData = (TextView) view.findViewById(R.id.tvData);
    ImageView ivConnection = (ImageView) view.findViewById(R.id.ivConnection);
    ImageView ivInteractive = (ImageView) view.findViewById(R.id.ivInteractive);

    // Show time
    tvTime.setText(new SimpleDateFormat("HH:mm:ss").format(time));

    // Show connection type
    if (connection <= 0)
        ivConnection.setImageResource(allowed > 0 ? R.drawable.host_allowed : R.drawable.host_blocked);
    else {
        if (allowed > 0)
            ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_on : R.drawable.other_on);
        else
            ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_off : R.drawable.other_off);
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Drawable wrap = DrawableCompat.wrap(ivConnection.getDrawable());
        DrawableCompat.setTint(wrap, allowed > 0 ? colorOn : colorOff);
    }

    // Show if screen on
    if (interactive <= 0)
        ivInteractive.setImageDrawable(null);
    else {
        ivInteractive.setImageResource(R.drawable.screen_on);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrap = DrawableCompat.wrap(ivInteractive.getDrawable());
            DrawableCompat.setTint(wrap, colorOn);
        }
    }

    // Show protocol name
    tvProtocol.setText(Util.getProtocolName(protocol, version, false));

    // SHow TCP flags
    tvFlags.setText(flags);
    tvFlags.setVisibility(TextUtils.isEmpty(flags) ? View.GONE : View.VISIBLE);

    // Show source and destination port
    if (protocol == 6 || protocol == 17) {
        tvSPort.setText(sport < 0 ? "" : getKnownPort(sport));
        tvDPort.setText(dport < 0 ? "" : getKnownPort(dport));
    } else {
        tvSPort.setText(sport < 0 ? "" : Integer.toString(sport));
        tvDPort.setText(dport < 0 ? "" : Integer.toString(dport));
    }

    // Application icon
    ApplicationInfo info = null;
    PackageManager pm = context.getPackageManager();
    String[] pkg = pm.getPackagesForUid(uid);
    if (pkg != null && pkg.length > 0)
        try {
            info = pm.getApplicationInfo(pkg[0], 0);
        } catch (PackageManager.NameNotFoundException ignored) {
        }
    if (info == null)
        ivIcon.setImageDrawable(null);
    else if (info.icon == 0)
        Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(ivIcon);
    else {
        Uri uri = Uri.parse("android.resource://" + info.packageName + "/" + info.icon);
        Picasso.with(context).load(uri).resize(iconSize, iconSize).into(ivIcon);
    }

    // https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h
    uid = uid % 100000; // strip off user ID
    if (uid == -1)
        tvUid.setText("");
    else if (uid == 0)
        tvUid.setText(context.getString(R.string.title_root));
    else if (uid == 9999)
        tvUid.setText("-"); // nobody
    else
        tvUid.setText(Integer.toString(uid));

    // Show source address
    tvSAddr.setText(getKnownAddress(saddr));

    // Show destination address
    if (resolve && !isKnownAddress(daddr))
        if (dname == null) {
            tvDaddr.setText(daddr);
            new AsyncTask<String, Object, String>() {
                @Override
                protected void onPreExecute() {
                    ViewCompat.setHasTransientState(tvDaddr, true);
                }

                @Override
                protected String doInBackground(String... args) {
                    try {
                        return InetAddress.getByName(args[0]).getHostName();
                    } catch (UnknownHostException ignored) {
                        return args[0];
                    }
                }

                @Override
                protected void onPostExecute(String name) {
                    tvDaddr.setText(">" + name);
                    ViewCompat.setHasTransientState(tvDaddr, false);
                }
            }.execute(daddr);
        } else
            tvDaddr.setText(dname);
    else
        tvDaddr.setText(getKnownAddress(daddr));

    // Show organization
    tvOrganization.setVisibility(View.GONE);
    if (organization) {
        if (!isKnownAddress(daddr))
            new AsyncTask<String, Object, String>() {
                @Override
                protected void onPreExecute() {
                    ViewCompat.setHasTransientState(tvOrganization, true);
                }

                @Override
                protected String doInBackground(String... args) {
                    try {
                        return Util.getOrganization(args[0]);
                    } catch (Throwable ex) {
                        Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                        return null;
                    }
                }

                @Override
                protected void onPostExecute(String organization) {
                    if (organization != null) {
                        tvOrganization.setText(organization);
                        tvOrganization.setVisibility(View.VISIBLE);
                    }
                    ViewCompat.setHasTransientState(tvOrganization, false);
                }
            }.execute(daddr);
    }

    // Show extra data
    if (TextUtils.isEmpty(data)) {
        tvData.setText("");
        tvData.setVisibility(View.GONE);
    } else {
        tvData.setText(data);
        tvData.setVisibility(View.VISIBLE);
    }
}