Search in sources :

Example 1 with TopApps

use of com.farmerbb.taskbar.util.TopApps 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)

Example 2 with TopApps

use of com.farmerbb.taskbar.util.TopApps in project Taskbar by farmerbb.

the class ReceiveSettingsReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    // Ignore this broadcast if this is the free version
    if (BuildConfig.APPLICATION_ID.equals(BuildConfig.PAID_APPLICATION_ID)) {
        // Get pinned and blocked apps
        PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
        pba.clear(context);
        String[] pinnedAppsPackageNames = intent.getStringArrayExtra("pinned_apps_package_names");
        String[] pinnedAppsComponentNames = intent.getStringArrayExtra("pinned_apps_component_names");
        String[] pinnedAppsLabels = intent.getStringArrayExtra("pinned_apps_labels");
        long[] pinnedAppsUserIds = intent.getLongArrayExtra("pinned_apps_user_ids");
        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
        if (pinnedAppsPackageNames != null && pinnedAppsComponentNames != null && pinnedAppsLabels != null)
            for (int i = 0; i < pinnedAppsPackageNames.length; i++) {
                Intent throwaway = new Intent();
                throwaway.setComponent(ComponentName.unflattenFromString(pinnedAppsComponentNames[i]));
                long userId;
                if (pinnedAppsUserIds != null)
                    userId = pinnedAppsUserIds[i];
                else
                    userId = userManager.getSerialNumberForUser(Process.myUserHandle());
                AppEntry newEntry = new AppEntry(pinnedAppsPackageNames[i], pinnedAppsComponentNames[i], pinnedAppsLabels[i], IconCache.getInstance(context).getIcon(context, context.getPackageManager(), launcherApps.resolveActivity(throwaway, userManager.getUserForSerialNumber(userId))), true);
                newEntry.setUserId(userId);
                pba.addPinnedApp(context, newEntry);
            }
        String[] blockedAppsPackageNames = intent.getStringArrayExtra("blocked_apps_package_names");
        String[] blockedAppsComponentNames = intent.getStringArrayExtra("blocked_apps_component_names");
        String[] blockedAppsLabels = intent.getStringArrayExtra("blocked_apps_labels");
        if (blockedAppsPackageNames != null && blockedAppsComponentNames != null && blockedAppsLabels != null)
            for (int i = 0; i < blockedAppsPackageNames.length; i++) {
                pba.addBlockedApp(context, new AppEntry(blockedAppsPackageNames[i], blockedAppsComponentNames[i], blockedAppsLabels[i], null, false));
            }
        // Get blacklist
        Blacklist blacklist = Blacklist.getInstance(context);
        blacklist.clear(context);
        String[] blacklistPackageNames = intent.getStringArrayExtra("blacklist_package_names");
        String[] blacklistLabels = intent.getStringArrayExtra("blacklist_labels");
        if (blacklistPackageNames != null && blacklistLabels != null)
            for (int i = 0; i < blacklistPackageNames.length; i++) {
                blacklist.addBlockedApp(context, new BlacklistEntry(blacklistPackageNames[i], blacklistLabels[i]));
            }
        // Get top apps
        TopApps topApps = TopApps.getInstance(context);
        topApps.clear(context);
        String[] topAppsPackageNames = intent.getStringArrayExtra("top_apps_package_names");
        String[] topAppsLabels = intent.getStringArrayExtra("top_apps_labels");
        if (topAppsPackageNames != null && topAppsLabels != null)
            for (int i = 0; i < topAppsPackageNames.length; i++) {
                topApps.addTopApp(context, new BlacklistEntry(topAppsPackageNames[i], topAppsLabels[i]));
            }
        // Get saved window sizes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
            savedWindowSizes.clear(context);
            String[] savedWindowSizesComponentNames = intent.getStringArrayExtra("saved_window_sizes_component_names");
            String[] savedWindowSizesWindowSizes = intent.getStringArrayExtra("saved_window_sizes_window_sizes");
            if (savedWindowSizesComponentNames != null && savedWindowSizesWindowSizes != null)
                for (int i = 0; i < savedWindowSizesComponentNames.length; i++) {
                    savedWindowSizes.setWindowSize(context, savedWindowSizesComponentNames[i], savedWindowSizesWindowSizes[i]);
                }
        }
        // Get shared preferences
        String contents = intent.getStringExtra("preferences");
        if (contents.length() > 0)
            try {
                File file = new File(context.getFilesDir().getParent() + "/shared_prefs/" + BuildConfig.APPLICATION_ID + "_preferences.xml");
                FileOutputStream output = new FileOutputStream(file);
                output.write(contents.getBytes());
                output.close();
            } catch (IOException e) {
            /* Gracefully fail */
            }
        try {
            File file = new File(context.getFilesDir() + File.separator + "imported_successfully");
            if (file.createNewFile())
                LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("com.farmerbb.taskbar.IMPORT_FINISHED"));
        } catch (IOException e) {
        /* Gracefully fail */
        }
    }
}
Also used : SavedWindowSizes(com.farmerbb.taskbar.util.SavedWindowSizes) LauncherApps(android.content.pm.LauncherApps) Intent(android.content.Intent) IOException(java.io.IOException) AppEntry(com.farmerbb.taskbar.util.AppEntry) BlacklistEntry(com.farmerbb.taskbar.util.BlacklistEntry) UserManager(android.os.UserManager) FileOutputStream(java.io.FileOutputStream) TopApps(com.farmerbb.taskbar.util.TopApps) Blacklist(com.farmerbb.taskbar.util.Blacklist) PinnedBlockedApps(com.farmerbb.taskbar.util.PinnedBlockedApps) File(java.io.File)

Example 3 with TopApps

use of com.farmerbb.taskbar.util.TopApps in project Taskbar by farmerbb.

the class SendSettingsReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    // Ignore this broadcast if this is the paid version
    if (BuildConfig.APPLICATION_ID.equals(BuildConfig.BASE_APPLICATION_ID)) {
        Intent sendSettingsIntent = new Intent("com.farmerbb.taskbar.SEND_SETTINGS");
        sendSettingsIntent.setPackage(BuildConfig.PAID_APPLICATION_ID);
        // Get pinned and blocked apps
        PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
        List<AppEntry> pinnedAppsList = pba.getPinnedApps();
        String[] pinnedAppsPackageNames = new String[pinnedAppsList.size()];
        String[] pinnedAppsComponentNames = new String[pinnedAppsList.size()];
        String[] pinnedAppsLabels = new String[pinnedAppsList.size()];
        long[] pinnedAppsUserIds = new long[pinnedAppsList.size()];
        for (int i = 0; i < pinnedAppsList.size(); i++) {
            AppEntry entry = pinnedAppsList.get(i);
            pinnedAppsPackageNames[i] = entry.getPackageName();
            pinnedAppsComponentNames[i] = entry.getComponentName();
            pinnedAppsLabels[i] = entry.getLabel();
            pinnedAppsUserIds[i] = entry.getUserId(context);
        }
        sendSettingsIntent.putExtra("pinned_apps_package_names", pinnedAppsPackageNames);
        sendSettingsIntent.putExtra("pinned_apps_component_names", pinnedAppsComponentNames);
        sendSettingsIntent.putExtra("pinned_apps_labels", pinnedAppsLabels);
        sendSettingsIntent.putExtra("pinned_apps_user_ids", pinnedAppsUserIds);
        List<AppEntry> blockedAppsList = pba.getBlockedApps();
        String[] blockedAppsPackageNames = new String[blockedAppsList.size()];
        String[] blockedAppsComponentNames = new String[blockedAppsList.size()];
        String[] blockedAppsLabels = new String[blockedAppsList.size()];
        for (int i = 0; i < blockedAppsList.size(); i++) {
            AppEntry entry = blockedAppsList.get(i);
            blockedAppsPackageNames[i] = entry.getPackageName();
            blockedAppsComponentNames[i] = entry.getComponentName();
            blockedAppsLabels[i] = entry.getLabel();
        }
        sendSettingsIntent.putExtra("blocked_apps_package_names", blockedAppsPackageNames);
        sendSettingsIntent.putExtra("blocked_apps_component_names", blockedAppsComponentNames);
        sendSettingsIntent.putExtra("blocked_apps_labels", blockedAppsLabels);
        // Get blacklist
        Blacklist blacklist = Blacklist.getInstance(context);
        List<BlacklistEntry> blacklistList = blacklist.getBlockedApps();
        String[] blacklistPackageNames = new String[blacklistList.size()];
        String[] blacklistLabels = new String[blacklistList.size()];
        for (int i = 0; i < blacklistList.size(); i++) {
            BlacklistEntry entry = blacklistList.get(i);
            blacklistPackageNames[i] = entry.getPackageName();
            blacklistLabels[i] = entry.getLabel();
        }
        sendSettingsIntent.putExtra("blacklist_package_names", blacklistPackageNames);
        sendSettingsIntent.putExtra("blacklist_labels", blacklistLabels);
        // Get top apps
        TopApps topApps = TopApps.getInstance(context);
        List<BlacklistEntry> topAppsList = topApps.getTopApps();
        String[] topAppsPackageNames = new String[topAppsList.size()];
        String[] topAppsLabels = new String[topAppsList.size()];
        for (int i = 0; i < topAppsList.size(); i++) {
            BlacklistEntry entry = topAppsList.get(i);
            topAppsPackageNames[i] = entry.getPackageName();
            topAppsLabels[i] = entry.getLabel();
        }
        sendSettingsIntent.putExtra("top_apps_package_names", topAppsPackageNames);
        sendSettingsIntent.putExtra("top_apps_labels", topAppsLabels);
        // Get saved window sizes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
            List<SavedWindowSizesEntry> savedWindowSizesList = savedWindowSizes.getSavedWindowSizes();
            String[] savedWindowSizesComponentNames = new String[savedWindowSizesList.size()];
            String[] savedWindowSizesWindowSizes = new String[savedWindowSizesList.size()];
            for (int i = 0; i < savedWindowSizesList.size(); i++) {
                SavedWindowSizesEntry entry = savedWindowSizesList.get(i);
                savedWindowSizesComponentNames[i] = entry.getComponentName();
                savedWindowSizesWindowSizes[i] = entry.getWindowSize();
            }
            sendSettingsIntent.putExtra("saved_window_sizes_component_names", savedWindowSizesComponentNames);
            sendSettingsIntent.putExtra("saved_window_sizes_window_sizes", savedWindowSizesWindowSizes);
        }
        // Get shared preferences
        StringBuilder preferences = new StringBuilder("");
        try {
            File file = new File(context.getFilesDir().getParent() + "/shared_prefs/" + BuildConfig.APPLICATION_ID + "_preferences.xml");
            FileInputStream input = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(input);
            BufferedReader buffer = new BufferedReader(reader);
            String line = buffer.readLine();
            while (line != null) {
                preferences.append(line);
                line = buffer.readLine();
                if (line != null)
                    preferences.append("\n");
            }
            reader.close();
        } catch (IOException e) {
        /* Gracefully fail */
        }
        sendSettingsIntent.putExtra("preferences", preferences.toString());
        // Finally, send the broadcast
        context.sendBroadcast(sendSettingsIntent);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SavedWindowSizes(com.farmerbb.taskbar.util.SavedWindowSizes) Intent(android.content.Intent) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AppEntry(com.farmerbb.taskbar.util.AppEntry) BlacklistEntry(com.farmerbb.taskbar.util.BlacklistEntry) SavedWindowSizesEntry(com.farmerbb.taskbar.util.SavedWindowSizesEntry) BufferedReader(java.io.BufferedReader) TopApps(com.farmerbb.taskbar.util.TopApps) Blacklist(com.farmerbb.taskbar.util.Blacklist) PinnedBlockedApps(com.farmerbb.taskbar.util.PinnedBlockedApps) File(java.io.File)

Aggregations

AppEntry (com.farmerbb.taskbar.util.AppEntry)3 Blacklist (com.farmerbb.taskbar.util.Blacklist)3 TopApps (com.farmerbb.taskbar.util.TopApps)3 Intent (android.content.Intent)2 LauncherApps (android.content.pm.LauncherApps)2 UserManager (android.os.UserManager)2 BlacklistEntry (com.farmerbb.taskbar.util.BlacklistEntry)2 PinnedBlockedApps (com.farmerbb.taskbar.util.PinnedBlockedApps)2 SavedWindowSizes (com.farmerbb.taskbar.util.SavedWindowSizes)2 File (java.io.File)2 IOException (java.io.IOException)2 ComponentName (android.content.ComponentName)1 SharedPreferences (android.content.SharedPreferences)1 LauncherActivityInfo (android.content.pm.LauncherActivityInfo)1 Drawable (android.graphics.drawable.Drawable)1 Handler (android.os.Handler)1 UserHandle (android.os.UserHandle)1 StartMenuAdapter (com.farmerbb.taskbar.adapter.StartMenuAdapter)1 SavedWindowSizesEntry (com.farmerbb.taskbar.util.SavedWindowSizesEntry)1 BufferedReader (java.io.BufferedReader)1