Search in sources :

Example 16 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project Taskbar by farmerbb.

the class TaskbarController method generateAppEntries.

@VisibleForTesting
void generateAppEntries(Context context, int number, List<AppEntry> usageStatsList6, List<AppEntry> entries, List<LauncherActivityInfo> launcherAppCache) {
    UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
    final List<UserHandle> userHandles = userManager.getUserProfiles();
    final String googleSearchBoxPackage = "com.google.android.googlequicksearchbox";
    final String googleSearchBoxActivity = "com.google.android.googlequicksearchbox.SearchActivity";
    for (int i = 0; i < number; i++) {
        for (UserHandle handle : userHandles) {
            String packageName = usageStatsList6.get(i).getPackageName();
            long lastTimeUsed = usageStatsList6.get(i).getLastTimeUsed();
            List<LauncherActivityInfo> list = launcherApps.getActivityList(packageName, handle);
            if (!list.isEmpty()) {
                // Google App workaround
                if (!packageName.equals(googleSearchBoxPackage)) {
                    launcherAppCache.add(list.get(0));
                } else {
                    boolean added = false;
                    for (LauncherActivityInfo info : list) {
                        if (info.getName().equals(googleSearchBoxActivity)) {
                            launcherAppCache.add(info);
                            added = true;
                        }
                    }
                    if (!added) {
                        launcherAppCache.add(list.get(0));
                    }
                }
                AppEntry newEntry = new AppEntry(packageName, null, null, null, false);
                newEntry.setUserId(userManager.getSerialNumberForUser(handle));
                newEntry.setLastTimeUsed(lastTimeUsed);
                entries.add(newEntry);
                break;
            }
        }
    }
}
Also used : AppEntry(com.farmerbb.taskbar.util.AppEntry) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) LauncherApps(android.content.pm.LauncherApps) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 17 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project Taskbar by farmerbb.

the class TaskbarController method populateAppEntries.

@SuppressWarnings("SuspiciousListRemoveInLoop")
@VisibleForTesting
void populateAppEntries(Context context, PackageManager pm, List<AppEntry> entries, List<LauncherActivityInfo> launcherAppCache) {
    UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
    int launcherAppCachePos = -1;
    for (int i = 0; i < entries.size(); i++) {
        if (entries.get(i).getComponentName() == null) {
            launcherAppCachePos++;
            LauncherActivityInfo appInfo = launcherAppCache.get(launcherAppCachePos);
            String packageName = entries.get(i).getPackageName();
            long lastTimeUsed = entries.get(i).getLastTimeUsed();
            entries.remove(i);
            AppEntry newEntry = new AppEntry(packageName, appInfo.getComponentName().flattenToString(), appInfo.getLabel().toString(), IconCache.getInstance(context).getIcon(context, pm, appInfo), false);
            newEntry.setUserId(userManager.getSerialNumberForUser(appInfo.getUser()));
            newEntry.setLastTimeUsed(lastTimeUsed);
            entries.add(i, newEntry);
        }
    }
}
Also used : AppEntry(com.farmerbb.taskbar.util.AppEntry) UserManager(android.os.UserManager) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 18 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project Taskbar by farmerbb.

the class U method refreshPinnedIcons.

public static void refreshPinnedIcons(Context context) {
    IconCache.getInstance(context).clearCache();
    PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
    List<AppEntry> pinnedAppsList = new ArrayList<>(pba.getPinnedApps());
    List<AppEntry> blockedAppsList = new ArrayList<>(pba.getBlockedApps());
    PackageManager pm = context.getPackageManager();
    pba.clear(context);
    for (AppEntry entry : pinnedAppsList) {
        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
        final List<UserHandle> userHandles = userManager.getUserProfiles();
        LauncherActivityInfo appInfo = null;
        for (UserHandle handle : userHandles) {
            List<LauncherActivityInfo> list = launcherApps.getActivityList(entry.getPackageName(), handle);
            if (!list.isEmpty()) {
                // Google App workaround
                if (!entry.getPackageName().equals("com.google.android.googlequicksearchbox"))
                    appInfo = list.get(0);
                else {
                    boolean added = false;
                    for (LauncherActivityInfo info : list) {
                        if (info.getName().equals("com.google.android.googlequicksearchbox.SearchActivity")) {
                            appInfo = info;
                            added = true;
                        }
                    }
                    if (!added)
                        appInfo = list.get(0);
                }
                break;
            }
        }
        if (appInfo != null) {
            AppEntry newEntry = new AppEntry(entry.getPackageName(), entry.getComponentName(), entry.getLabel(), IconCache.getInstance(context).getIcon(context, pm, appInfo), true);
            newEntry.setUserId(entry.getUserId(context));
            pba.addPinnedApp(context, newEntry);
        }
    }
    for (AppEntry entry : blockedAppsList) {
        pba.addBlockedApp(context, entry);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) ArrayList(java.util.ArrayList) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) LauncherApps(android.content.pm.LauncherApps)

Example 19 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project Taskbar by farmerbb.

the class FavoriteAppTileService method updateState.

private void updateState() {
    Tile tile = getQsTile();
    if (tile == null)
        return;
    SharedPreferences pref = U.getSharedPreferences(this);
    if (pref.getBoolean(prefix + PREF_ADDED_SUFFIX, false)) {
        tile.setState(Tile.STATE_ACTIVE);
        tile.setLabel(pref.getString(prefix + PREF_LABEL_SUFFIX, getString(R.string.tb_new_shortcut)));
        String componentName = pref.getString(prefix + PREF_COMPONENT_NAME_SUFFIX, null);
        float threshold = pref.getFloat(prefix + PREF_ICON_THRESHOLD_SUFFIX, -1);
        if (componentName != null && threshold >= 0) {
            UserManager userManager = (UserManager) getSystemService(USER_SERVICE);
            LauncherApps launcherApps = (LauncherApps) getSystemService(LAUNCHER_APPS_SERVICE);
            long userId = pref.getLong(prefix + PREF_USER_ID_SUFFIX, userManager.getSerialNumberForUser(Process.myUserHandle()));
            Intent intent = new Intent();
            intent.setComponent(ComponentName.unflattenFromString(componentName));
            LauncherActivityInfo info = launcherApps.resolveActivity(intent, userManager.getUserForSerialNumber(userId));
            IconCache cache = IconCache.getInstance(this);
            BitmapDrawable icon = U.convertToMonochrome(this, cache.getIcon(this, info), threshold);
            tile.setIcon(Icon.createWithBitmap(icon.getBitmap()));
        } else {
            tile.setIcon(Icon.createWithResource(this, R.drawable.tb_favorite_app_tile));
        }
    } else {
        tile.setState(Tile.STATE_INACTIVE);
        tile.setLabel(getString(R.string.tb_new_shortcut));
        tile.setIcon(Icon.createWithResource(this, R.drawable.tb_favorite_app_tile));
    }
    tile.updateTile();
}
Also used : SharedPreferences(android.content.SharedPreferences) UserManager(android.os.UserManager) IconCache(com.farmerbb.taskbar.util.IconCache) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) Tile(android.service.quicksettings.Tile) LauncherApps(android.content.pm.LauncherApps) Intent(android.content.Intent) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Example 20 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project Taskbar by farmerbb.

the class StartMenuService method refreshApps.

private void refreshApps(final String query, final boolean firstDraw) {
    if (thread != null)
        thread.interrupt();
    handler = new Handler();
    thread = new Thread(() -> {
        if (pm == null)
            pm = getPackageManager();
        UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
        LauncherApps launcherApps = (LauncherApps) getSystemService(Context.LAUNCHER_APPS_SERVICE);
        final List<UserHandle> userHandles = userManager.getUserProfiles();
        final List<LauncherActivityInfo> unfilteredList = new ArrayList<>();
        for (UserHandle handle : userHandles) {
            unfilteredList.addAll(launcherApps.getActivityList(null, handle));
        }
        final List<LauncherActivityInfo> topAppsList = new ArrayList<>();
        final List<LauncherActivityInfo> allAppsList = new ArrayList<>();
        final List<LauncherActivityInfo> list = new ArrayList<>();
        TopApps topApps = TopApps.getInstance(StartMenuService.this);
        for (LauncherActivityInfo appInfo : unfilteredList) {
            if (topApps.isTopApp(appInfo.getComponentName().flattenToString()) || topApps.isTopApp(appInfo.getName()))
                topAppsList.add(appInfo);
        }
        Blacklist blacklist = Blacklist.getInstance(StartMenuService.this);
        for (LauncherActivityInfo appInfo : unfilteredList) {
            if (!(blacklist.isBlocked(appInfo.getComponentName().flattenToString()) || blacklist.isBlocked(appInfo.getName())) && !(topApps.isTopApp(appInfo.getComponentName().flattenToString()) || topApps.isTopApp(appInfo.getName())))
                allAppsList.add(appInfo);
        }
        Collections.sort(topAppsList, comparator);
        Collections.sort(allAppsList, comparator);
        list.addAll(topAppsList);
        list.addAll(allAppsList);
        topAppsList.clear();
        allAppsList.clear();
        List<LauncherActivityInfo> queryList;
        if (query == null)
            queryList = list;
        else {
            queryList = new ArrayList<>();
            for (LauncherActivityInfo appInfo : list) {
                if (appInfo.getLabel().toString().toLowerCase().contains(query.toLowerCase()))
                    queryList.add(appInfo);
            }
        }
        // Now that we've generated the list of apps,
        // we need to determine if we need to redraw the start menu or not
        boolean shouldRedrawStartMenu = false;
        List<String> finalApplicationIds = new ArrayList<>();
        if (query == null && !firstDraw) {
            for (LauncherActivityInfo appInfo : queryList) {
                finalApplicationIds.add(appInfo.getApplicationInfo().packageName);
            }
            if (finalApplicationIds.size() != currentStartMenuIds.size())
                shouldRedrawStartMenu = true;
            else {
                for (int i = 0; i < finalApplicationIds.size(); i++) {
                    if (!finalApplicationIds.get(i).equals(currentStartMenuIds.get(i))) {
                        shouldRedrawStartMenu = true;
                        break;
                    }
                }
            }
        } else
            shouldRedrawStartMenu = true;
        if (shouldRedrawStartMenu) {
            if (query == null)
                currentStartMenuIds = finalApplicationIds;
            Drawable defaultIcon = pm.getDefaultActivityIcon();
            final List<AppEntry> entries = new ArrayList<>();
            for (LauncherActivityInfo appInfo : queryList) {
                // Attempt to work around frequently reported OutOfMemoryErrors
                String label;
                Drawable icon;
                try {
                    label = appInfo.getLabel().toString();
                    icon = IconCache.getInstance(StartMenuService.this).getIcon(StartMenuService.this, pm, appInfo);
                } catch (OutOfMemoryError e) {
                    System.gc();
                    label = appInfo.getApplicationInfo().packageName;
                    icon = defaultIcon;
                }
                AppEntry newEntry = new AppEntry(appInfo.getApplicationInfo().packageName, new ComponentName(appInfo.getApplicationInfo().packageName, appInfo.getName()).flattenToString(), label, icon, false);
                newEntry.setUserId(userManager.getSerialNumberForUser(appInfo.getUser()));
                entries.add(newEntry);
            }
            handler.post(() -> {
                String queryText = searchView.getQuery().toString();
                if (query == null && queryText.length() == 0 || query != null && query.equals(queryText)) {
                    StartMenuAdapter adapter;
                    SharedPreferences pref = U.getSharedPreferences(StartMenuService.this);
                    if (pref.getString("start_menu_layout", "list").equals("grid")) {
                        startMenu.setNumColumns(3);
                        adapter = new StartMenuAdapter(StartMenuService.this, R.layout.row_alt, entries);
                    } else
                        adapter = new StartMenuAdapter(StartMenuService.this, R.layout.row, entries);
                    int position = startMenu.getFirstVisiblePosition();
                    startMenu.setAdapter(adapter);
                    startMenu.setSelection(position);
                    if (adapter.getCount() > 0)
                        textView.setText(null);
                    else if (query != null)
                        textView.setText(getString(Patterns.WEB_URL.matcher(query).matches() ? R.string.press_enter_alt : R.string.press_enter));
                    else
                        textView.setText(getString(R.string.nothing_to_see_here));
                }
            });
        }
    });
    thread.start();
}
Also used : SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) Drawable(android.graphics.drawable.Drawable) Handler(android.os.Handler) LauncherApps(android.content.pm.LauncherApps) AppEntry(com.farmerbb.taskbar.util.AppEntry) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) TopApps(com.farmerbb.taskbar.util.TopApps) List(java.util.List) ArrayList(java.util.ArrayList) ComponentName(android.content.ComponentName) Blacklist(com.farmerbb.taskbar.util.Blacklist) StartMenuAdapter(com.farmerbb.taskbar.adapter.StartMenuAdapter)

Aggregations

LauncherActivityInfo (android.content.pm.LauncherActivityInfo)82 LauncherApps (android.content.pm.LauncherApps)38 ArrayList (java.util.ArrayList)31 UserHandle (android.os.UserHandle)26 Intent (android.content.Intent)20 ComponentName (android.content.ComponentName)15 List (java.util.List)13 UserManager (android.os.UserManager)12 Test (org.junit.Test)11 Point (android.graphics.Point)10 PackageManager (android.content.pm.PackageManager)9 ResolveInfo (android.content.pm.ResolveInfo)9 Config (org.robolectric.annotation.Config)9 SuppressLint (android.annotation.SuppressLint)8 AppInfo (com.android.launcher3.model.data.AppInfo)8 Context (android.content.Context)7 SharedPreferences (android.content.SharedPreferences)7 ShortcutInfo (android.content.pm.ShortcutInfo)7 AppInfo (com.android.launcher3.AppInfo)7 SessionInfo (android.content.pm.PackageInstaller.SessionInfo)6