Search in sources :

Example 1 with UserHandle

use of fr.neamar.kiss.utils.UserHandle in project KISS by Neamar.

the class LoadAppPojos method doInBackground.

@Override
protected ArrayList<AppPojo> doInBackground(Void... params) {
    long start = System.nanoTime();
    ArrayList<AppPojo> apps = new ArrayList<>();
    String excludedAppList = PreferenceManager.getDefaultSharedPreferences(context).getString("excluded-apps-list", context.getPackageName() + ";");
    List excludedApps = Arrays.asList(excludedAppList.split(";"));
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UserManager manager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        LauncherApps launcher = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
        // Handle multi-profile support introduced in Android 5 (#542)
        for (android.os.UserHandle profile : manager.getUserProfiles()) {
            UserHandle user = new UserHandle(manager.getSerialNumberForUser(profile), profile);
            for (LauncherActivityInfo activityInfo : launcher.getActivityList(null, profile)) {
                ApplicationInfo appInfo = activityInfo.getApplicationInfo();
                String fullPackageName = user.addUserSuffixToString(appInfo.packageName, '#');
                if (!excludedApps.contains(fullPackageName)) {
                    AppPojo app = new AppPojo();
                    app.id = user.addUserSuffixToString(pojoScheme + appInfo.packageName + "/" + activityInfo.getName(), '/');
                    app.setName(activityInfo.getLabel().toString());
                    app.packageName = appInfo.packageName;
                    app.activityName = activityInfo.getName();
                    // Wrap Android user handle in opaque container that will work across
                    // all Android versions
                    app.userHandle = user;
                    app.setTags(tagsHandler.getTags(app.id));
                    apps.add(app);
                }
            }
        }
    } else {
        PackageManager manager = context.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        for (ResolveInfo info : manager.queryIntentActivities(mainIntent, 0)) {
            ApplicationInfo appInfo = info.activityInfo.applicationInfo;
            if (!excludedApps.contains(appInfo.packageName)) {
                AppPojo app = new AppPojo();
                app.id = pojoScheme + appInfo.packageName + "/" + info.activityInfo.name;
                app.setName(info.loadLabel(manager).toString());
                app.packageName = appInfo.packageName;
                app.activityName = info.activityInfo.name;
                app.userHandle = new UserHandle();
                app.setTags(tagsHandler.getTags(app.id));
                apps.add(app);
            }
        }
    }
    // Apply app sorting preference
    if (prefs.getString("sort-apps", "alphabetical").equals("invertedAlphabetical")) {
        Collections.sort(apps, Collections.reverseOrder(new AppPojo.NameComparator()));
    } else {
        Collections.sort(apps, new AppPojo.NameComparator());
    }
    long end = System.nanoTime();
    Log.i("time", Long.toString((end - start) / 1000000) + " milliseconds to list apps");
    return apps;
}
Also used : AppPojo(fr.neamar.kiss.pojo.AppPojo) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) LauncherApps(android.content.pm.LauncherApps) Intent(android.content.Intent) ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) UserManager(android.os.UserManager) UserHandle(fr.neamar.kiss.utils.UserHandle) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with UserHandle

use of fr.neamar.kiss.utils.UserHandle in project KISS by Neamar.

the class AppProvider method onCreate.

@Override
public void onCreate() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Package installation/uninstallation events for the main
        // profile are still handled using PackageAddedRemovedHandler itself
        final UserManager manager = (UserManager) this.getSystemService(Context.USER_SERVICE);
        final LauncherApps launcher = (LauncherApps) this.getSystemService(Context.LAUNCHER_APPS_SERVICE);
        launcher.registerCallback(new LauncherApps.Callback() {

            @Override
            public void onPackageAdded(String packageName, android.os.UserHandle user) {
                if (!Process.myUserHandle().equals(user)) {
                    PackageAddedRemovedHandler.handleEvent(AppProvider.this, "android.intent.action.PACKAGE_ADDED", packageName, new UserHandle(manager.getSerialNumberForUser(user), user), false);
                }
            }

            @Override
            public void onPackageChanged(String packageName, android.os.UserHandle user) {
                if (!Process.myUserHandle().equals(user)) {
                    PackageAddedRemovedHandler.handleEvent(AppProvider.this, "android.intent.action.PACKAGE_ADDED", packageName, new UserHandle(manager.getSerialNumberForUser(user), user), true);
                }
            }

            @Override
            public void onPackageRemoved(String packageName, android.os.UserHandle user) {
                if (!Process.myUserHandle().equals(user)) {
                    PackageAddedRemovedHandler.handleEvent(AppProvider.this, "android.intent.action.PACKAGE_REMOVED", packageName, new UserHandle(manager.getSerialNumberForUser(user), user), false);
                }
            }

            @Override
            public void onPackagesAvailable(String[] packageNames, android.os.UserHandle user, boolean replacing) {
                if (!Process.myUserHandle().equals(user)) {
                    PackageAddedRemovedHandler.handleEvent(AppProvider.this, "android.intent.action.MEDIA_MOUNTED", null, new UserHandle(manager.getSerialNumberForUser(user), user), false);
                }
            }

            @Override
            public void onPackagesUnavailable(String[] packageNames, android.os.UserHandle user, boolean replacing) {
                if (!Process.myUserHandle().equals(user)) {
                    PackageAddedRemovedHandler.handleEvent(AppProvider.this, "android.intent.action.MEDIA_UNMOUNTED", null, new UserHandle(manager.getSerialNumberForUser(user), user), false);
                }
            }
        });
        // Try to clean up app-related data when profile is removed
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
        filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
        this.registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_ADDED)) {
                    AppProvider.this.reload();
                } else if (intent.getAction().equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) {
                    android.os.UserHandle profile = (android.os.UserHandle) intent.getParcelableExtra(Intent.EXTRA_USER);
                    UserHandle user = new UserHandle(manager.getSerialNumberForUser(profile), profile);
                    KissApplication.getDataHandler(context).removeFromExcluded(user);
                    KissApplication.getDataHandler(context).removeFromFavorites(user);
                    AppProvider.this.reload();
                }
            }
        }, filter);
    }
    super.onCreate();
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) LoadAppPojos(fr.neamar.kiss.loader.LoadAppPojos) LauncherApps(android.content.pm.LauncherApps) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) UserManager(android.os.UserManager) UserHandle(fr.neamar.kiss.utils.UserHandle)

Aggregations

Intent (android.content.Intent)2 LauncherApps (android.content.pm.LauncherApps)2 UserManager (android.os.UserManager)2 UserHandle (fr.neamar.kiss.utils.UserHandle)2 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 IntentFilter (android.content.IntentFilter)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 LauncherActivityInfo (android.content.pm.LauncherActivityInfo)1 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 LoadAppPojos (fr.neamar.kiss.loader.LoadAppPojos)1 AppPojo (fr.neamar.kiss.pojo.AppPojo)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1