Search in sources :

Example 71 with IActivityManager

use of android.app.IActivityManager in project android_frameworks_base by AOSPA.

the class SettingsCmd method run.

public void run() {
    boolean valid = false;
    String arg;
    try {
        while ((arg = nextArg()) != null) {
            if ("--user".equals(arg)) {
                if (mUser != -1) {
                    // --user specified more than once; invalid
                    break;
                }
                arg = nextArg();
                if ("current".equals(arg) || "cur".equals(arg)) {
                    mUser = UserHandle.USER_CURRENT;
                } else {
                    mUser = Integer.parseInt(arg);
                }
            } else if (mVerb == CommandVerb.UNSPECIFIED) {
                if ("get".equalsIgnoreCase(arg)) {
                    mVerb = CommandVerb.GET;
                } else if ("put".equalsIgnoreCase(arg)) {
                    mVerb = CommandVerb.PUT;
                } else if ("delete".equalsIgnoreCase(arg)) {
                    mVerb = CommandVerb.DELETE;
                } else if ("list".equalsIgnoreCase(arg)) {
                    mVerb = CommandVerb.LIST;
                } else {
                    // invalid
                    System.err.println("Invalid command: " + arg);
                    break;
                }
            } else if (mTable == null) {
                if (!"system".equalsIgnoreCase(arg) && !"secure".equalsIgnoreCase(arg) && !"global".equalsIgnoreCase(arg)) {
                    System.err.println("Invalid namespace '" + arg + "'");
                    // invalid
                    break;
                }
                mTable = arg.toLowerCase();
                if (mVerb == CommandVerb.LIST) {
                    valid = true;
                    break;
                }
            } else if (mVerb == CommandVerb.GET || mVerb == CommandVerb.DELETE) {
                mKey = arg;
                if (mNextArg >= mArgs.length) {
                    valid = true;
                } else {
                    System.err.println("Too many arguments");
                }
                break;
            } else if (mKey == null) {
                mKey = arg;
            // keep going; there's another PUT arg
            } else {
                // PUT, final arg
                mValue = arg;
                if (mNextArg >= mArgs.length) {
                    valid = true;
                } else {
                    System.err.println("Too many arguments");
                }
                break;
            }
        }
    } catch (Exception e) {
        valid = false;
    }
    if (valid) {
        try {
            IActivityManager activityManager = ActivityManagerNative.getDefault();
            if (mUser == UserHandle.USER_CURRENT) {
                mUser = activityManager.getCurrentUser().id;
            }
            if (mUser < 0) {
                mUser = UserHandle.USER_SYSTEM;
            }
            IContentProvider provider = null;
            IBinder token = new Binder();
            try {
                ContentProviderHolder holder = activityManager.getContentProviderExternal("settings", UserHandle.USER_SYSTEM, token);
                if (holder == null) {
                    throw new IllegalStateException("Could not find settings provider");
                }
                provider = holder.provider;
                switch(mVerb) {
                    case GET:
                        System.out.println(getForUser(provider, mUser, mTable, mKey));
                        break;
                    case PUT:
                        putForUser(provider, mUser, mTable, mKey, mValue);
                        break;
                    case DELETE:
                        System.out.println("Deleted " + deleteForUser(provider, mUser, mTable, mKey) + " rows");
                        break;
                    case LIST:
                        for (String line : listForUser(provider, mUser, mTable)) {
                            System.out.println(line);
                        }
                        break;
                    default:
                        System.err.println("Unspecified command");
                        break;
                }
            } finally {
                if (provider != null) {
                    activityManager.removeContentProviderExternal("settings", token);
                }
            }
        } catch (Exception e) {
            System.err.println("Error while accessing settings provider");
            e.printStackTrace();
        }
    } else {
        printUsage();
    }
}
Also used : IBinder(android.os.IBinder) Binder(android.os.Binder) IBinder(android.os.IBinder) IContentProvider(android.content.IContentProvider) ContentProviderHolder(android.app.IActivityManager.ContentProviderHolder) RemoteException(android.os.RemoteException) IActivityManager(android.app.IActivityManager)

Example 72 with IActivityManager

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

the class DevUtils method killForegroundApplication.

/**
     * Kills the top most / most recent user application, but leaves out the launcher.
     * This is function governed by {@link Settings.Secure.KILL_APP_LONGPRESS_BACK}.
     *
     * @param context the current context, used to retrieve the package manager.
     * @return {@code true} when a user application was found and closed.
     */
public static boolean killForegroundApplication(Context context) {
    boolean targetKilled = false;
    try {
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        String defaultHomePackage = "com.android.launcher";
        intent.addCategory(Intent.CATEGORY_HOME);
        final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);
        if (res.activityInfo != null && !res.activityInfo.packageName.equals("android")) {
            defaultHomePackage = res.activityInfo.packageName;
        }
        IActivityManager am = ActivityManagerNative.getDefault();
        List<RunningAppProcessInfo> apps = am.getRunningAppProcesses();
        for (RunningAppProcessInfo appInfo : apps) {
            int uid = appInfo.uid;
            // root, phone, etc.)
            if (uid >= Process.FIRST_APPLICATION_UID && uid <= Process.LAST_APPLICATION_UID && appInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appInfo.pkgList != null && (appInfo.pkgList.length > 0)) {
                    for (String pkg : appInfo.pkgList) {
                        if (!pkg.equals("com.android.systemui") && !pkg.equals(defaultHomePackage)) {
                            am.forceStopPackage(pkg, UserHandle.USER_CURRENT);
                            targetKilled = true;
                            break;
                        }
                    }
                } else {
                    Process.killProcess(appInfo.pid);
                    targetKilled = true;
                    break;
                }
            }
        }
    } catch (RemoteException remoteException) {
    // Do nothing; just let it go.
    }
    return targetKilled;
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) Intent(android.content.Intent) RemoteException(android.os.RemoteException) IActivityManager(android.app.IActivityManager)

Example 73 with IActivityManager

use of android.app.IActivityManager in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class Utils method getInsecureTargetUser.

/**
     * Returns the target user for a Settings activity.
     *
     * The target user can be either the current user, the user that launched this activity or
     * the user contained as an extra in the arguments or intent extras.
     *
     * You should use {@link #getSecureTargetUser(IBinder, UserManager, Bundle, Bundle)} if
     * possible.
     *
     * @see #getInsecureTargetUser(IBinder, Bundle, Bundle)
     */
public static UserHandle getInsecureTargetUser(IBinder activityToken, @Nullable Bundle arguments, @Nullable Bundle intentExtras) {
    UserHandle currentUser = new UserHandle(UserHandle.myUserId());
    IActivityManager am = ActivityManagerNative.getDefault();
    try {
        UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId(am.getLaunchedFromUid(activityToken)));
        if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) {
            return launchedFromUser;
        }
        UserHandle extrasUser = intentExtras != null ? (UserHandle) intentExtras.getParcelable(EXTRA_USER) : null;
        if (extrasUser != null && !extrasUser.equals(currentUser)) {
            return extrasUser;
        }
        UserHandle argumentsUser = arguments != null ? (UserHandle) arguments.getParcelable(EXTRA_USER) : null;
        if (argumentsUser != null && !argumentsUser.equals(currentUser)) {
            return argumentsUser;
        }
    } catch (RemoteException e) {
        // Should not happen
        Log.v(TAG, "Could not talk to activity manager.", e);
        return null;
    }
    return currentUser;
}
Also used : UserHandle(android.os.UserHandle) RemoteException(android.os.RemoteException) IActivityManager(android.app.IActivityManager)

Example 74 with IActivityManager

use of android.app.IActivityManager in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class Utils method getSecureTargetUser.

/**
     * Returns the target user for a Settings activity.
     * <p>
     * User would be retrieved in this order:
     * <ul>
     * <li> If this activity is launched from other user, return that user id.
     * <li> If this is launched from the Settings app in same user, return the user contained as an
     *      extra in the arguments or intent extras.
     * <li> Otherwise, return UserHandle.myUserId().
     * </ul>
     * <p>
     * Note: This is secure in the sense that it only returns a target user different to the current
     * one if the app launching this activity is the Settings app itself, running in the same user
     * or in one that is in the same profile group, or if the user id is provided by the system.
     */
public static UserHandle getSecureTargetUser(IBinder activityToken, UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) {
    UserHandle currentUser = new UserHandle(UserHandle.myUserId());
    IActivityManager am = ActivityManagerNative.getDefault();
    try {
        String launchedFromPackage = am.getLaunchedFromPackage(activityToken);
        boolean launchedFromSettingsApp = SETTINGS_PACKAGE_NAME.equals(launchedFromPackage);
        UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId(am.getLaunchedFromUid(activityToken)));
        if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) {
            // Check it's secure
            if (isProfileOf(um, launchedFromUser)) {
                return launchedFromUser;
            }
        }
        UserHandle extrasUser = getUserHandleFromBundle(intentExtras);
        if (extrasUser != null && !extrasUser.equals(currentUser)) {
            // Check it's secure
            if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) {
                return extrasUser;
            }
        }
        UserHandle argumentsUser = getUserHandleFromBundle(arguments);
        if (argumentsUser != null && !argumentsUser.equals(currentUser)) {
            // Check it's secure
            if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) {
                return argumentsUser;
            }
        }
    } catch (RemoteException e) {
        // Should not happen
        Log.v(TAG, "Could not talk to activity manager.", e);
    }
    return currentUser;
}
Also used : UserHandle(android.os.UserHandle) SpannableString(android.text.SpannableString) RemoteException(android.os.RemoteException) IActivityManager(android.app.IActivityManager)

Example 75 with IActivityManager

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

the class Dpm method parseArgs.

private void parseArgs(boolean canHaveName) {
    String opt;
    while ((opt = nextOption()) != null) {
        if ("--user".equals(opt)) {
            String arg = nextArgRequired();
            if ("current".equals(arg) || "cur".equals(arg)) {
                mUserId = UserHandle.USER_CURRENT;
            } else {
                mUserId = parseInt(arg);
            }
            if (mUserId == UserHandle.USER_CURRENT) {
                IActivityManager activityManager = ActivityManagerNative.getDefault();
                try {
                    mUserId = activityManager.getCurrentUser().id;
                } catch (RemoteException e) {
                    e.rethrowAsRuntimeException();
                }
            }
        } else if (canHaveName && "--name".equals(opt)) {
            mName = nextArgRequired();
        } else {
            throw new IllegalArgumentException("Unknown option: " + opt);
        }
    }
    mComponent = parseComponentName(nextArgRequired());
}
Also used : RemoteException(android.os.RemoteException) IActivityManager(android.app.IActivityManager)

Aggregations

IActivityManager (android.app.IActivityManager)85 RemoteException (android.os.RemoteException)72 Intent (android.content.Intent)21 IBinder (android.os.IBinder)18 Configuration (android.content.res.Configuration)14 ContentProviderHolder (android.app.IActivityManager.ContentProviderHolder)11 IContentProvider (android.content.IContentProvider)11 Binder (android.os.Binder)11 ActivityThread (android.app.ActivityThread)7 Locale (java.util.Locale)7 BroadcastReceiver (android.content.BroadcastReceiver)6 Context (android.content.Context)6 IMountService (android.os.storage.IMountService)6 IMountShutdownObserver (android.os.storage.IMountShutdownObserver)6 Cursor (android.database.Cursor)5 ComponentName (android.content.ComponentName)4 PackageManagerInternal (android.content.pm.PackageManagerInternal)4 UserInfo (android.content.pm.UserInfo)4 ErrnoException (android.system.ErrnoException)4 ArrayMap (android.util.ArrayMap)4