/* * Copyright (C) 2012 www.amsoft.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.yzy.supercleanmaster.utils; // TODO: Auto-generated Javadoc import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.IPackageStatsObserver; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageStats; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Looper; import android.os.RemoteException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; /** * */ public class Utils { public static boolean isSystemApp(PackageInfo pInfo) { return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); } public static boolean isSystemUpdateApp(PackageInfo pInfo) { return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); } public static boolean isUserApp(PackageInfo pInfo) { return (!isSystemApp(pInfo) && !isSystemUpdateApp(pInfo)); } public static long getPkgSize(final Context context, String pkgName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { long pkgSize=0; // getPackageSizeInfo是PackageManager中的一个private方法,所以需要通过反射的机制来调用 Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class, IPackageStatsObserver.class}); // 调用 getPackageSizeInfo 方法,需要两个参数:1、需要检测的应用包名;2、回调 method.invoke(context.getPackageManager(), new Object[]{ pkgName, new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // 子线程中默认无法处理消息循环,自然也就不能显示Toast,所以需要手动Looper一下 Looper.prepare(); // 从pStats中提取各个所需数据 // pkgSize= pStats.cacheSize+pStats.dataSize+pStats.codeSize; // Toast.makeText(context, // "缓存大小=" + Formatter.formatFileSize(context, pStats.cacheSize) + // "\n数据大小=" + Formatter.formatFileSize(context, pStats.dataSize) + // "\n程序大小=" + Formatter.formatFileSize(context, pStats.codeSize), // Toast.LENGTH_LONG).show(); // 遍历一次消息队列,弹出Toast Looper.loop(); } } }); return pkgSize; } public static void launchBrowser(Activity from, String url) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri content_url = Uri.parse(url); intent.setData(content_url); from.startActivity(intent); } public static boolean isIntentSafe(Activity activity, Uri uri) { Intent mapCall = new Intent(Intent.ACTION_VIEW, uri); PackageManager packageManager = activity.getPackageManager(); List<ResolveInfo> activities = packageManager.queryIntentActivities(mapCall, 0); return activities.size() > 0; } }