Search in sources :

Example 36 with Application

use of android.app.Application in project android_frameworks_base by ResurrectionRemix.

the class DpiTestActivity method init.

public void init(boolean noCompat) {
    try {
        // This is all a dirty hack.  Don't think a real application should
        // be doing it.
        Application app = ActivityThread.currentActivityThread().getApplication();
        ApplicationInfo ai = app.getPackageManager().getApplicationInfo("com.google.android.test.dpi", 0);
        if (noCompat) {
            ai.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS | ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS | ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS | ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS | ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS | ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
            app.getResources().setCompatibilityInfo(new CompatibilityInfo(ai, getResources().getConfiguration().screenLayout, getResources().getConfiguration().smallestScreenWidthDp, false));
        }
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("ouch", e);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) CompatibilityInfo(android.content.res.CompatibilityInfo) Application(android.app.Application)

Example 37 with Application

use of android.app.Application in project LshUtils by SenhLinsh.

the class LshToastUtils method showToast.

private static void showToast(Context context, String text, int duration) {
    Toast toast;
    if (context instanceof Application) {
        toast = sToast.get();
        if (toast == null) {
            toast = Toast.makeText(context, text, duration);
            sToast = new SoftReference<>(toast);
        } else {
            toast.setDuration(duration);
        }
    } else {
        toast = Toast.makeText(context, text, duration);
    }
    toast.setText(text);
    toast.show();
}
Also used : Toast(android.widget.Toast) Application(android.app.Application)

Example 38 with Application

use of android.app.Application in project android_frameworks_base by ResurrectionRemix.

the class RemoteViews method getApplicationInfo.

private static ApplicationInfo getApplicationInfo(String packageName, int userId) {
    if (packageName == null) {
        return null;
    }
    // Get the application for the passed in package and user.
    Application application = ActivityThread.currentApplication();
    if (application == null) {
        throw new IllegalStateException("Cannot create remote views out of an aplication.");
    }
    ApplicationInfo applicationInfo = application.getApplicationInfo();
    if (UserHandle.getUserId(applicationInfo.uid) != userId || !applicationInfo.packageName.equals(packageName)) {
        try {
            Context context = application.getBaseContext().createPackageContextAsUser(packageName, 0, new UserHandle(userId));
            applicationInfo = context.getApplicationInfo();
        } catch (NameNotFoundException nnfe) {
            throw new IllegalArgumentException("No such package " + packageName);
        }
    }
    return applicationInfo;
}
Also used : Context(android.content.Context) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) UserHandle(android.os.UserHandle) ApplicationInfo(android.content.pm.ApplicationInfo) Application(android.app.Application)

Example 39 with Application

use of android.app.Application in project DroidPlugin by DroidPluginTeam.

the class PluginProcessManager method getPluginContext.

public static Application getPluginContext(String packageName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
    if (!sApplicationsCache.containsKey(packageName)) {
        Object at = ActivityThreadCompat.currentActivityThread();
        Object mAllApplications = FieldUtils.readField(at, "mAllApplications");
        if (mAllApplications instanceof List) {
            List apps = (List) mAllApplications;
            for (Object o : apps) {
                if (o instanceof Application) {
                    Application app = (Application) o;
                    if (!sApplicationsCache.containsKey(app.getPackageName())) {
                        sApplicationsCache.put(app.getPackageName(), app);
                    }
                }
            }
        }
    }
    return sApplicationsCache.get(packageName);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) Application(android.app.Application)

Example 40 with Application

use of android.app.Application in project CustomActivityOnCrash by Ereza.

the class CustomActivityOnCrash method install.

/**
     * Installs CustomActivityOnCrash on the application using the default error activity.
     *
     * @param context Context to use for obtaining the ApplicationContext. Must not be null.
     */
public static void install(Context context) {
    try {
        if (context == null) {
            Log.e(TAG, "Install failed: context is null!");
        } else {
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                Log.w(TAG, "CustomActivityOnCrash will be installed, but may not be reliable in API lower than 14");
            }
            //INSTALL!
            final Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();
            if (oldHandler != null && oldHandler.getClass().getName().startsWith(CAOC_HANDLER_PACKAGE_NAME)) {
                Log.e(TAG, "You have already installed CustomActivityOnCrash, doing nothing!");
            } else {
                if (oldHandler != null && !oldHandler.getClass().getName().startsWith(DEFAULT_HANDLER_PACKAGE_NAME)) {
                    Log.e(TAG, "IMPORTANT WARNING! You already have an UncaughtExceptionHandler, are you sure this is correct? If you use ACRA, Crashlytics or similar libraries, you must initialize them AFTER CustomActivityOnCrash! Installing anyway, but your original handler will not be called.");
                }
                application = (Application) context.getApplicationContext();
                //We define a default exception handler that does what we want so it can be called from Crashlytics/ACRA
                Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

                    @Override
                    public void uncaughtException(Thread thread, final Throwable throwable) {
                        Log.e(TAG, "App has crashed, executing CustomActivityOnCrash's UncaughtExceptionHandler", throwable);
                        if (hasCrashedInTheLastSeconds(application)) {
                            Log.e(TAG, "App already crashed in the last 2 seconds, not starting custom error activity because we could enter a restart loop. Are you sure that your app does not crash directly on init?", throwable);
                            if (oldHandler != null) {
                                oldHandler.uncaughtException(thread, throwable);
                                return;
                            }
                        } else {
                            setLastCrashTimestamp(application, new Date().getTime());
                            if (errorActivityClass == null) {
                                errorActivityClass = guessErrorActivityClass(application);
                            }
                            if (isStackTraceLikelyConflictive(throwable, errorActivityClass)) {
                                Log.e(TAG, "Your application class or your error activity have crashed, the custom activity will not be launched!");
                                if (oldHandler != null) {
                                    oldHandler.uncaughtException(thread, throwable);
                                    return;
                                }
                            } else if (launchErrorActivityWhenInBackground || !isInBackground) {
                                final Intent intent = new Intent(application, errorActivityClass);
                                StringWriter sw = new StringWriter();
                                PrintWriter pw = new PrintWriter(sw);
                                throwable.printStackTrace(pw);
                                String stackTraceString = sw.toString();
                                //And: http://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception#comment46697371_12809171
                                if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) {
                                    String disclaimer = " [stack trace too large]";
                                    stackTraceString = stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length()) + disclaimer;
                                }
                                if (enableAppRestart && restartActivityClass == null) {
                                    //We can set the restartActivityClass because the app will terminate right now,
                                    //and when relaunched, will be null again by default.
                                    restartActivityClass = guessRestartActivityClass(application);
                                } else if (!enableAppRestart) {
                                    //In case someone sets the activity and then decides to not restart
                                    restartActivityClass = null;
                                }
                                intent.putExtra(EXTRA_STACK_TRACE, stackTraceString);
                                intent.putExtra(EXTRA_RESTART_ACTIVITY_CLASS, restartActivityClass);
                                intent.putExtra(EXTRA_SHOW_ERROR_DETAILS, showErrorDetails);
                                intent.putExtra(EXTRA_EVENT_LISTENER, eventListener);
                                intent.putExtra(EXTRA_IMAGE_DRAWABLE_ID, defaultErrorActivityDrawableId);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                if (eventListener != null) {
                                    eventListener.onLaunchErrorActivity();
                                }
                                application.startActivity(intent);
                            }
                        }
                        final Activity lastActivity = lastActivityCreated.get();
                        if (lastActivity != null) {
                            //We finish the activity, this solves a bug which causes infinite recursion.
                            //This is unsolvable in API<14, so beware!
                            //See: https://github.com/ACRA/acra/issues/42
                            lastActivity.finish();
                            lastActivityCreated.clear();
                        }
                        killCurrentProcess();
                    }
                });
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

                        int currentlyStartedActivities = 0;

                        @Override
                        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                            if (activity.getClass() != errorActivityClass) {
                                // Copied from ACRA:
                                // Ignore activityClass because we want the last
                                // application Activity that was started so that we can
                                // explicitly kill it off.
                                lastActivityCreated = new WeakReference<>(activity);
                            }
                        }

                        @Override
                        public void onActivityStarted(Activity activity) {
                            currentlyStartedActivities++;
                            isInBackground = (currentlyStartedActivities == 0);
                        //Do nothing
                        }

                        @Override
                        public void onActivityResumed(Activity activity) {
                        //Do nothing
                        }

                        @Override
                        public void onActivityPaused(Activity activity) {
                        //Do nothing
                        }

                        @Override
                        public void onActivityStopped(Activity activity) {
                            //Do nothing
                            currentlyStartedActivities--;
                            isInBackground = (currentlyStartedActivities == 0);
                        }

                        @Override
                        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                        //Do nothing
                        }

                        @Override
                        public void onActivityDestroyed(Activity activity) {
                        //Do nothing
                        }
                    });
                }
                Log.i(TAG, "CustomActivityOnCrash has been installed.");
            }
        }
    } catch (Throwable t) {
        Log.e(TAG, "An unknown error occurred while installing CustomActivityOnCrash, it may not have been properly initialized. Please report this as a bug if needed.", t);
    }
}
Also used : Bundle(android.os.Bundle) DefaultErrorActivity(cat.ereza.customactivityoncrash.activity.DefaultErrorActivity) Activity(android.app.Activity) Intent(android.content.Intent) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint) StringWriter(java.io.StringWriter) WeakReference(java.lang.ref.WeakReference) Application(android.app.Application) PrintWriter(java.io.PrintWriter)

Aggregations

Application (android.app.Application)125 Test (org.junit.Test)27 Context (android.content.Context)24 Activity (android.app.Activity)23 ApplicationInfo (android.content.pm.ApplicationInfo)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 File (java.io.File)10 MvpPresenter (com.hannesdorfmann.mosby3.mvp.MvpPresenter)8 Before (org.junit.Before)8 ShadowApplication (org.robolectric.shadows.ShadowApplication)8 MvpView (com.hannesdorfmann.mosby3.mvp.MvpView)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)6 PrintWriter (java.io.PrintWriter)6 Date (java.util.Date)6 SuppressLint (android.annotation.SuppressLint)5 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 RemoteException (android.os.RemoteException)5 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)5 AndroidRuntimeException (android.util.AndroidRuntimeException)5