use of android.content.pm.PackageManager.NameNotFoundException in project AndroidTraining by mixi-inc.
the class SuggestionsAdapter method getActivityIcon.
/**
* Gets the activity or application icon for an activity.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the acitivy or the application
* have an icon set.
*/
private Drawable getActivityIcon(ComponentName component) {
PackageManager pm = mContext.getPackageManager();
final ActivityInfo activityInfo;
try {
activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
} catch (NameNotFoundException ex) {
Log.w(LOG_TAG, ex.toString());
return null;
}
int iconId = activityInfo.getIconResource();
if (iconId == 0)
return null;
String pkg = component.getPackageName();
Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
if (drawable == null) {
Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " + component.flattenToShortString());
return null;
}
return drawable;
}
use of android.content.pm.PackageManager.NameNotFoundException in project SmartAndroidSource by jaychou2012.
the class SuggestionsAdapter method getTheDrawable.
public Drawable getTheDrawable(Uri uri) throws FileNotFoundException {
String authority = uri.getAuthority();
Resources r;
if (TextUtils.isEmpty(authority)) {
throw new FileNotFoundException("No authority: " + uri);
} else {
try {
r = mContext.getPackageManager().getResourcesForApplication(authority);
} catch (NameNotFoundException ex) {
throw new FileNotFoundException("No package found for authority: " + uri);
}
}
List<String> path = uri.getPathSegments();
if (path == null) {
throw new FileNotFoundException("No path: " + uri);
}
int len = path.size();
int id;
if (len == 1) {
try {
id = Integer.parseInt(path.get(0));
} catch (NumberFormatException e) {
throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
}
} else if (len == 2) {
id = r.getIdentifier(path.get(1), path.get(0), authority);
} else {
throw new FileNotFoundException("More than two path segments: " + uri);
}
if (id == 0) {
throw new FileNotFoundException("No resource found for: " + uri);
}
return r.getDrawable(id);
}
use of android.content.pm.PackageManager.NameNotFoundException in project SmartAndroidSource by jaychou2012.
the class Volley method newRequestQueue.
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
use of android.content.pm.PackageManager.NameNotFoundException in project XPrivacy by M66B.
the class PrivacyService method onDemandDialog.
// Helper methods
private OnDemandResult onDemandDialog(final Hook hook, final PRestriction restriction, final PRestriction result) {
final OnDemandResult oResult = new OnDemandResult();
try {
int userId = Util.getUserId(restriction.uid);
// Check if application
if (!PrivacyManager.isApplication(restriction.uid))
if (!getSettingBool(userId, PrivacyManager.cSettingOnDemandSystem, false))
return oResult;
// Check for exceptions
if (hook != null && !hook.canOnDemand())
return oResult;
if (!PrivacyManager.canRestrict(restriction.uid, getXUid(), restriction.restrictionName, restriction.methodName, false))
return oResult;
// Check if enabled
if (!getSettingBool(userId, PrivacyManager.cSettingOnDemand, true))
return oResult;
if (!getSettingBool(restriction.uid, PrivacyManager.cSettingOnDemand, false))
return oResult;
// Check version
String version = getSetting(new PSetting(userId, "", PrivacyManager.cSettingVersion, "0.0")).value;
if (new Version(version).compareTo(new Version("2.1.5")) < 0)
return oResult;
// Get activity manager context
final Context context = getContext();
if (context == null)
return oResult;
long token = 0;
try {
token = Binder.clearCallingIdentity();
// Get application info
final ApplicationInfoEx appInfo = new ApplicationInfoEx(context, restriction.uid);
// Check for system application
if (appInfo.isSystem())
if (new Version(version).compareTo(new Version("2.0.38")) < 0)
return oResult;
// Check if activity manager agrees
if (!XActivityManagerService.canOnDemand())
return oResult;
// Check if activity manager locked
if (isAMLocked(restriction.uid)) {
Util.log(null, Log.WARN, "On demand locked " + restriction);
return oResult;
}
// Go ask
Util.log(null, Log.WARN, "On demand " + restriction);
mOndemandSemaphore.acquireUninterruptibly();
try {
// Check if activity manager still agrees
if (!XActivityManagerService.canOnDemand())
return oResult;
// Check if activity manager locked now
if (isAMLocked(restriction.uid)) {
Util.log(null, Log.WARN, "On demand acquired locked " + restriction);
return oResult;
}
Util.log(null, Log.WARN, "On demanding " + restriction);
// Check if method not asked before
CRestriction mkey = new CRestriction(restriction, null);
synchronized (mRestrictionCache) {
if (mRestrictionCache.containsKey(mkey)) {
CRestriction mrestriction = mRestrictionCache.get(mkey);
if (mrestriction.asked) {
Util.log(null, Log.WARN, "Already asked " + restriction);
result.restricted = mrestriction.restricted;
result.asked = true;
return oResult;
}
}
}
// Check if category not asked before (once)
CRestriction ckey = new CRestriction(restriction, null);
ckey.setMethodName(null);
synchronized (mAskedOnceCache) {
if (mAskedOnceCache.containsKey(ckey)) {
CRestriction carestriction = mAskedOnceCache.get(ckey);
if (!carestriction.isExpired()) {
Util.log(null, Log.WARN, "Already asked once category " + restriction);
result.restricted = carestriction.restricted;
result.asked = true;
return oResult;
}
}
}
// Check if method not asked before once
synchronized (mAskedOnceCache) {
if (mAskedOnceCache.containsKey(mkey)) {
CRestriction marestriction = mAskedOnceCache.get(mkey);
if (!marestriction.isExpired()) {
Util.log(null, Log.WARN, "Already asked once method " + restriction);
result.restricted = marestriction.restricted;
result.asked = true;
return oResult;
}
}
}
// Check if whitelist not asked before
if (restriction.extra != null && hook != null && hook.whitelist() != null) {
CSetting skey = new CSetting(restriction.uid, hook.whitelist(), restriction.extra);
synchronized (mSettingCache) {
if (mSettingCache.containsKey(skey)) {
String value = mSettingCache.get(skey).getValue();
if (value != null) {
Util.log(null, Log.WARN, "Already asked whitelist " + skey);
result.restricted = Boolean.parseBoolean(value);
result.asked = true;
return oResult;
}
}
for (String xextra : getXExtra(restriction, hook)) {
CSetting xkey = new CSetting(restriction.uid, hook.whitelist(), xextra);
if (mSettingCache.containsKey(xkey)) {
String value = mSettingCache.get(xkey).getValue();
if (value != null) {
Util.log(null, Log.WARN, "Already asked whitelist " + xkey);
result.restricted = Boolean.parseBoolean(value);
result.asked = true;
return oResult;
}
}
}
}
}
final OnDemandDialogHolder holder = new OnDemandDialogHolder();
// Build dialog parameters
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
params.dimAmount = 0.85f;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.TRANSLUCENT;
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;
params.gravity = Gravity.CENTER;
// Get window manager
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
// Show dialog
mHandler.post(new Runnable() {
@Override
public void run() {
try {
// Build dialog
holder.dialog = getOnDemandView(restriction, hook, appInfo, result, context, holder, oResult);
// Handle reset button
((Button) holder.dialog.findViewById(R.id.btnReset)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((ProgressBar) holder.dialog.findViewById(R.id.pbProgress)).setProgress(cMaxOnDemandDialog * 20);
holder.reset = true;
holder.latch.countDown();
}
});
// Make dialog visible
wm.addView(holder.dialog, params);
// Update progress bar
Runnable runProgress = new Runnable() {
@Override
public void run() {
if (holder.dialog != null && holder.dialog.isShown()) {
// Update progress bar
ProgressBar progressBar = (ProgressBar) holder.dialog.findViewById(R.id.pbProgress);
if (progressBar.getProgress() > 0) {
progressBar.incrementProgressBy(-1);
mHandler.postDelayed(this, 50);
}
// Check if activity manager locked
if (isAMLocked(restriction.uid)) {
Util.log(null, Log.WARN, "On demand dialog locked " + restriction);
((Button) holder.dialog.findViewById(R.id.btnDontKnow)).callOnClick();
}
}
}
};
mHandler.postDelayed(runProgress, 50);
// Enabled buttons after one second
boolean repeat = (SystemClock.elapsedRealtime() - mOnDemandLastAnswer < 1000);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (holder.dialog != null && holder.dialog.isShown()) {
holder.dialog.findViewById(R.id.btnAllow).setEnabled(true);
holder.dialog.findViewById(R.id.btnDontKnow).setEnabled(true);
holder.dialog.findViewById(R.id.btnDeny).setEnabled(true);
}
}
}, repeat ? 0 : 1000);
} catch (NameNotFoundException ex) {
Util.log(null, Log.WARN, ex.toString());
} catch (Throwable ex) {
Util.bug(null, ex);
}
}
});
// Wait for choice, reset or timeout
do {
holder.reset = false;
boolean choice = holder.latch.await(cMaxOnDemandDialog, TimeUnit.SECONDS);
if (holder.reset) {
holder.latch = new CountDownLatch(1);
Util.log(null, Log.WARN, "On demand reset " + restriction);
} else if (choice)
oResult.ondemand = true;
else
Util.log(null, Log.WARN, "On demand timeout " + restriction);
} while (holder.reset);
mOnDemandLastAnswer = SystemClock.elapsedRealtime();
// Dismiss dialog
mHandler.post(new Runnable() {
@Override
public void run() {
View dialog = holder.dialog;
if (dialog != null)
wm.removeView(dialog);
}
});
} finally {
mOndemandSemaphore.release();
}
} finally {
Binder.restoreCallingIdentity(token);
}
} catch (Throwable ex) {
Util.bug(null, ex);
}
return oResult;
}
use of android.content.pm.PackageManager.NameNotFoundException in project XPrivacy by M66B.
the class Util method getProEnablerVersion.
public static Version getProEnablerVersion(Context context) {
try {
String proPackageName = context.getPackageName() + ".pro";
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(proPackageName, 0);
return new Version(pi.versionName);
} catch (NameNotFoundException ignored) {
} catch (Throwable ex) {
Util.bug(null, ex);
}
return null;
}
Aggregations