Java Code Examples for android.content.pm.PackageManager.getApplicationInfo()

The following are Jave code examples for showing how to use getApplicationInfo() of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: emerald   File: IconPackManager.java   View Source Code Vote up 6 votes
public Map<String, String> getIconPacks() {
	Map<String, String> iconPacks = new HashMap<String, String>();
	PackageManager pm = context.getPackageManager();
	List<ResolveInfo> iconPacksInfo = pm.queryIntentActivities(new Intent("org.adw.launcher.THEMES"), PackageManager.GET_META_DATA);
	String iconPackPackage = null;
	String iconPackName = null;
	for (ResolveInfo info: iconPacksInfo) {
		iconPackPackage = info.activityInfo.packageName;
		ApplicationInfo ai = null;
		try {
			ai = pm.getApplicationInfo(iconPackPackage, PackageManager.GET_META_DATA);
			iconPackName = pm.getApplicationLabel(ai).toString();
		} catch (PackageManager.NameNotFoundException e) {}
		iconPacks.put(iconPackName, iconPackPackage);
	}
	return iconPacks;
}
 
Example 2
Project: TPlayer   File: PackageParserEx.java   View Source Code Vote up 6 votes
public static void initApplicationInfoBase(PackageSetting ps, VPackage p) {
    ApplicationInfo ai = p.applicationInfo;
    ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
    if (TextUtils.isEmpty(ai.processName)) {
        ai.processName = ai.packageName;
    }
    ai.enabled = true;
    ai.nativeLibraryDir = ps.libPath;
    ai.uid = ps.appId;
    ai.name = ComponentFixer.fixComponentClassName(ps.packageName, ai.name);
    ai.publicSourceDir = ps.apkPath;
    ai.sourceDir = ps.apkPath;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ai.splitSourceDirs = new String[]{ps.apkPath};
        ai.splitPublicSourceDirs = ai.splitSourceDirs;
        ApplicationInfoL.scanSourceDir.set(ai, ai.dataDir);
        ApplicationInfoL.scanPublicSourceDir.set(ai, ai.dataDir);
        String hostPrimaryCpuAbi = ApplicationInfoL.primaryCpuAbi.get(VirtualCore.get().getContext().getApplicationInfo());
        ApplicationInfoL.primaryCpuAbi.set(ai, hostPrimaryCpuAbi);
    }

    if (ps.dependSystem) {
        String[] sharedLibraryFiles = sSharedLibCache.get(ps.packageName);
        if (sharedLibraryFiles == null) {
            PackageManager hostPM = VirtualCore.get().getUnHookPackageManager();
            try {
                ApplicationInfo hostInfo = hostPM.getApplicationInfo(ps.packageName, PackageManager.GET_SHARED_LIBRARY_FILES);
                sharedLibraryFiles = hostInfo.sharedLibraryFiles;
                if (sharedLibraryFiles == null) sharedLibraryFiles = new String[0];
                sSharedLibCache.put(ps.packageName, sharedLibraryFiles);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        ai.sharedLibraryFiles = sharedLibraryFiles;
    }
}
 
Example 3
Project: DinningShare   File: SystemWebViewClient.java   View Source Code Vote up 6 votes
/**
 * Notify the host application that an SSL error occurred while loading a resource.
 * The host application must call either handler.cancel() or handler.proceed().
 * Note that the decision may be retained for use in response to future SSL errors.
 * The default behavior is to cancel the load.
 *
 * @param view          The WebView that is initiating the callback.
 * @param handler       An SslErrorHandler object that will handle the user's response.
 * @param error         The SSL error object.
 */
@TargetApi(8)
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {

    final String packageName = parentEngine.cordova.getActivity().getPackageName();
    final PackageManager pm = parentEngine.cordova.getActivity().getPackageManager();

    ApplicationInfo appInfo;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
            // debug = true
            handler.proceed();
            return;
        } else {
            // debug = false
            super.onReceivedSslError(view, handler, error);
        }
    } catch (NameNotFoundException e) {
        // When it doubt, lock it out!
        super.onReceivedSslError(view, handler, error);
    }
}
 
Example 4
Project: 19porn   File: Kits.java   View Source Code Vote up 6 votes
/**
 * 判断是否是系统App
 *
 * @param context
 * @param packageName 包名
 * @return
 */
public static boolean isSystemApplication(Context context, String packageName) {
    if (context == null) {
        return false;
    }
    PackageManager packageManager = context.getPackageManager();
    if (packageManager == null || packageName == null || packageName.length() == 0) {
        return false;
    }

    try {
        ApplicationInfo app = packageManager.getApplicationInfo(packageName, 0);
        return (app != null && (app.flags & ApplicationInfo.FLAG_SYSTEM) > 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 5
Project: prevent   File: PreventFragment.java   View Source Code Vote up 6 votes
@Override
protected Set<AppInfo> doInBackground(Void... params) {
    PreventActivity pa = wr.get();
    Set<AppInfo> applications = new TreeSet<AppInfo>();
    if (pa == null) {
        return applications;
    }
    PackageManager pm = pa.getPackageManager();
    Map<String, Set<Long>> running = pa.getRunningProcesses();
    int i = 1;
    for (String name : mAdapter.getNames()) {
        publishProgress(++i);
        ApplicationInfo info;
        try {
            info = pm.getApplicationInfo(name, 0);
        } catch (NameNotFoundException e) { // NOSONAR
            info = null;
        }
        if (info == null || !info.enabled) {
            continue;
        }
        String label = labelLoader.loadLabel(info);
        applications.add(new AppInfo(name, label, running.get(name)).setFlags(info.flags));
    }
    return applications;
}
 
Example 6
Project: siiMobilityAppKit   File: SystemWebViewClient.java   View Source Code Vote up 6 votes
/**
 * Notify the host application that an SSL error occurred while loading a resource.
 * The host application must call either handler.cancel() or handler.proceed().
 * Note that the decision may be retained for use in response to future SSL errors.
 * The default behavior is to cancel the load.
 *
 * @param view          The WebView that is initiating the callback.
 * @param handler       An SslErrorHandler object that will handle the user's response.
 * @param error         The SSL error object.
 */
@TargetApi(8)
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {

    final String packageName = parentEngine.cordova.getActivity().getPackageName();
    final PackageManager pm = parentEngine.cordova.getActivity().getPackageManager();

    ApplicationInfo appInfo;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
            // debug = true
            handler.proceed();
            return;
        } else {
            // debug = false
            super.onReceivedSslError(view, handler, error);
        }
    } catch (NameNotFoundException e) {
        // When it doubt, lock it out!
        super.onReceivedSslError(view, handler, error);
    }
}
 
Example 7
Project: HeadlineNews   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * 判断App是否是系统应用
 *
 * @param packageName 包名
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isSystemApp(final String packageName) {
    if (isSpace(packageName)) return false;
    try {
        PackageManager pm = Utils.getContext().getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 8
Project: Trinity-App   File: MainActivity.java   View Source Code Vote up 5 votes
public static Intent newFacebookIntent(PackageManager pm, String url) {
    Uri uri = Uri.parse(url);
    try {
        ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
        if (applicationInfo.enabled) {
            // http://stackoverflow.com/a/24547437/1048340
            uri = Uri.parse("fb://facewebmodal/f?href="+url);
        }
    } catch (PackageManager.NameNotFoundException ignored) {
        facebook_not_found=true;
    }
    return new Intent(Intent.ACTION_VIEW, uri);
}
 
Example 9
Project: react-native-app-metadata   File: RNAppMetadataModule.java   View Source Code Vote up 5 votes
@ReactMethod
public void getAppMetadataBy(String key, Promise promise) {
    PackageManager pm = this.reactContext.getPackageManager();
    String value = null;
    try {
        ApplicationInfo pInfo = pm.getApplicationInfo(this.packageName, PackageManager.GET_META_DATA);
        value = pInfo.metaData.getString(key);
        promise.resolve(value);
    } catch (PackageManager.NameNotFoundException e) {
        //e.printStackTrace();
        Log.e("[RNAppMetadata]", " name not found");
        promise.resolve(e);
    }
}
 
Example 10
Project: Leanplum-Android-SDK   File: Util.java   View Source Code Vote up 5 votes
/**
 * Set update time from apk file modification time.
 */
private static void setUpdateTime(Map<String, Object> params, PackageManager packageManager,
    String packageName) {
  try {
    ApplicationInfo info = packageManager.getApplicationInfo(packageName, 0);
    File apkFile = new File(info.sourceDir);
    if (apkFile.exists()) {
      params.put(Constants.Params.UPDATE_DATE, "" + (apkFile.lastModified() / 1000.0));
    }
  } catch (Throwable t) {
    Log.w("Failed to find package info: " + t);
  }
}
 
Example 11
Project: lockit   File: HomeActivity.java   View Source Code Vote up 5 votes
@TargetApi(Build.VERSION_CODES.KITKAT)
private boolean hasUsageAccess() {
    try {
        PackageManager packageManager = getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);

    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 12
Project: androidtools   File: AppUtils.java   View Source Code Vote up 5 votes
/**
 * Gets the name of the application
 *
 * @param context     context
 * @param packagename Application package name
 * @return App name
 */
public String getAppName(Context context, String packagename) {
    //Package management operations management class
    PackageManager pm = context.getPackageManager();
    try {
        ApplicationInfo info = pm.getApplicationInfo(packagename, 0);
        return info.loadLabel(pm).toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();

    }
    return packagename;
}
 
Example 13
Project: XinFramework   File: SilentInstaller.java   View Source Code Vote up 5 votes
/**
 * /**
 * whether packageName is system application
 */
public boolean isSystemApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();
    if (packageManager == null || packageName == null || packageName.length() == 0) {
        return false;
    }
    try {
        ApplicationInfo app = packageManager.getApplicationInfo(packageName, 0);
        return (app != null && (app.flags & ApplicationInfo.FLAG_SYSTEM) > 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 14
Project: FriendBook   File: UpdateService.java   View Source Code Vote up 5 votes
private int getIcon(Context context) {

            final PackageManager packageManager = context.getPackageManager();
            ApplicationInfo appInfo = null;
            try {
                appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            if (appInfo != null) {
                return appInfo.icon;
            }
            return 0;
        }
 
Example 15
Project: AndroidModulePattern   File: Utils.java   View Source Code Vote up 5 votes
/**
 * 判断App是否是Debug版本
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isAppDebug() {
    if (StringUtils.isSpace(context.getPackageName())) return false;
    try {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);
        return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 16
Project: keepass2android   File: PluginManager.java   View Source Code Vote up 5 votes
Resources getResources(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Resources res = null;
    try {
        ApplicationInfo appInfo = packageManager.getApplicationInfo(mPackageName, 0);
        res = packageManager.getResourcesForApplication(appInfo);
    } catch (NameNotFoundException e) {
        Log.i(TAG, "couldn't get resources");
    }
    return res;
}
 
Example 17
Project: ModuleFrame   File: AppUtil.java   View Source Code Vote up 5 votes
/**
 * 获取Application中的meta-data.
 *
 * @param packageManager 应用管理
 * @param packageName 包名
 * @return Bundle
 */
public static Bundle getMetaDataFromApplication(PackageManager packageManager, String packageName) {
    Bundle bundle = null;
    try {
        ApplicationInfo ai = packageManager.getApplicationInfo(packageName,
                PackageManager.GET_META_DATA);
        bundle = ai.metaData;
    } catch (PackageManager.NameNotFoundException e) {
        LogUtil.logE(AppUtil.class, Log.getStackTraceString(e));
    }
    return bundle;
}
 
Example 18
Project: GitHub   File: ManifestUtil.java   View Source Code Vote up 4 votes
public static String getMetaDataValue(Context context, String name) {

        Object value = null;

        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo;

        try {

            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 128);
            if (applicationInfo != null && applicationInfo.metaData != null) {
                value = applicationInfo.metaData.get(name);
            }

        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Could not read the name in the manifest file.", e);
        }

        if (value == null) {
            throw new RuntimeException("The name '" + name+ "' is not defined in the manifest file's meta data.");
        }

        return value.toString();

    }
 
Example 19
Project: LaunchEnr   File: IconChooser.java   View Source Code Vote up 4 votes
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //apply activity's theme if dark theme is enabled
    SettingsTheme.apply(this);

    setContentView(R.layout.all_icons_view);

    mActionBar = getActionBar();

    mCurrentPackageName = getIntent().getStringExtra("app_package");
    mCurrentPackageLabel = getIntent().getStringExtra("app_label");
    mIconPackPackageName = getIntent().getStringExtra("icon_pack_package");

    PackageManager packageManager = getPackageManager();

    final View loadingView = findViewById(R.id.loading);

    final ImageView appIcon = findViewById(R.id.app_icon);

    ApplicationInfo iconPackInfo;

    try {
        iconPackInfo = packageManager.getApplicationInfo(mIconPackPackageName, 0);
        mActionBar.setSubtitle(packageManager.getApplicationLabel(iconPackInfo));
        appIcon.setImageDrawable(packageManager.getApplicationIcon(iconPackInfo));

    } catch (final PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    mIconCache = LauncherAppState.getInstance(this).getIconCache();
    mIconsManager = IconCache.getIconsManager(this);

    mIconsGrid = findViewById(R.id.icons_grid);
    mIconsGrid.setHasFixedSize(true);
    int itemSpacing = getResources().getDimensionPixelSize(R.dimen.grid_item_spacing);
    mIconsGrid.addItemDecoration(new GridItemSpacer(itemSpacing));

    mGridLayout = new GridLayoutManager(this, 4);
    mIconsGrid.setLayoutManager(mGridLayout);
    mIconsGrid.setAlpha(0.0f);
    mActionBar.hide();

    mIconSize = getResources().getDimensionPixelSize(R.dimen.icon_pack_icon_size);

    final Animator anim = AnimatorInflater
            .loadAnimator(this, R.animator.flip);
    anim.setTarget(loadingView);
    anim.start();

    new Thread(new Runnable() {
        @Override
        public void run() {

            allIcons =
                    mIconsManager.getAllDrawables(mIconPackPackageName);
            matchingIcons =
                    mIconsManager.getMatchingDrawables(mCurrentPackageName);
            IconChooser.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mGridAdapter = new GridAdapter(allIcons, matchingIcons);
                    mIconsGrid.setAdapter(mGridAdapter);
                    anim.cancel();
                    loadingView.setVisibility(View.GONE);

                    mIconsGrid.animate().alpha(1.0f);
                    mActionBar.show();
                }
            });
        }
    }).start();
}
 
Example 20
Project: AndroidUtilCode-master   File: ProcessUtils.java   View Source Code Vote up 4 votes
/**
 * 获取前台线程包名
 * <p>当不是查看当前App,且SDK大于21时,
 * 需添加权限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p>
 *
 * @return 前台应用包名
 */
public static String getForegroundProcessName() {
    ActivityManager manager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pInfo = manager.getRunningAppProcesses();
    if (pInfo != null && pInfo.size() != 0) {
        for (ActivityManager.RunningAppProcessInfo aInfo : pInfo) {
            if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return aInfo.processName;
            }
        }
    }
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
        PackageManager packageManager = Utils.getContext().getPackageManager();
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        System.out.println(list);
        if (list.size() > 0) {// 有"有权查看使用权限的应用"选项
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(Utils.getContext().getPackageName(), 0);
                AppOpsManager aom = (AppOpsManager) Utils.getContext().getSystemService(Context.APP_OPS_SERVICE);
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    Utils.getContext().startActivity(intent);
                }
                if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) != AppOpsManager.MODE_ALLOWED) {
                    LogUtils.d("getForegroundApp", "没有打开\"有权查看使用权限的应用\"选项");
                    return null;
                }
                UsageStatsManager usageStatsManager = (UsageStatsManager) Utils.getContext().getSystemService(Context.USAGE_STATS_SERVICE);
                long endTime = System.currentTimeMillis();
                long beginTime = endTime - 86400000 * 7;
                List<UsageStats> usageStatses = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
                if (usageStatses == null || usageStatses.isEmpty()) return null;
                UsageStats recentStats = null;
                for (UsageStats usageStats : usageStatses) {
                    if (recentStats == null || usageStats.getLastTimeUsed() > recentStats.getLastTimeUsed()) {
                        recentStats = usageStats;
                    }
                }
                return recentStats == null ? null : recentStats.getPackageName();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            LogUtils.d("getForegroundApp", "无\"有权查看使用权限的应用\"选项");
        }
    }
    return null;
}