Search in sources :

Example 71 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project emerald by HenriDellal.

the class GetApps method doInBackground.

@Override
protected ArrayList<AppData> doInBackground(Boolean... slow) {
    // Log.v("getting", "installed");
    ArrayList<AppData> apps = new ArrayList<AppData>();
    // use intent to get apps that can be launched
    Intent launchIntent = new Intent(Intent.ACTION_MAIN);
    launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    // TODO access shared preferences the other way
    int appShortcut = Integer.parseInt(context.options.getString(Keys.APP_SHORTCUT, "3"));
    boolean icons = appShortcut >= CustomAdapter.ICON;
    Map<String, AppData> cache = new HashMap<String, AppData>();
    // delete icons from cache if they aren't used
    if (slow[0] || !icons) {
        MyCache.deleteIcons(context);
    }
    // copy apps information into local cache variable
    if (!slow[0]) {
        ArrayList<AppData> cacheData = new ArrayList<AppData>();
        MyCache.read(context, CACHE_NAME, cacheData);
        // Log.v("TinyLaunch", "cache "+cacheData.size());
        for (AppData a : cacheData) {
            cache.put(a.getComponent(), a);
        }
    }
    // get list of app info from system (only those that can be launched)
    String name;
    boolean cacheValid;
    if (Build.VERSION.SDK_INT >= 21) {
        List<LauncherActivityInfo> list = ((LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE)).getActivityList(null, Process.myUserHandle());
        for (int i = 0; i < list.size(); i++) {
            // call function to update the progress bar
            publishProgress(i, list.size());
            // get info of app located by index i in list
            LauncherActivityInfo info = list.get(i);
            // get package name
            ComponentName cn = info.getComponentName();
            component = cn.flattenToString();
            name = null;
            cacheValid = false;
            // get app data from cache
            if (!slow[0]) {
                AppData a = cache.get(component);
                if (a != null) {
                    name = a.name;
                    cacheValid = true;
                }
            }
            // if cache is not valid then set name from package manager
            if (!cacheValid) {
                name = info.getLabel().toString();
                if (name == null)
                    name = component;
                if (name.equals("Emerald Launcher")) {
                    continue;
                }
            }
            // add new appdata object to apps list
            apps.add(new AppData(component, name));
            // load icons
            if (icons) {
                // get icon file for app from cache
                File iconFile = MyCache.getIconFile(context, component);
                // if there is no icon for app in cache
                if (!cacheValid || !iconFile.exists()) {
                    writeIconTo(iconFile, info.getIcon(0));
                }
            }
        }
        // when apps are retrieved
        // save apps list in cache
        MyCache.write(context, CACHE_NAME, apps);
        // clean icons of deleted apps
        MyCache.cleanIcons(context, apps);
        publishProgress(list.size(), list.size());
    } else {
        List<ResolveInfo> list = pm.queryIntentActivities(launchIntent, 0);
        for (int i = 0; i < list.size(); i++) {
            // call function to update the progress bar
            publishProgress(i, list.size());
            // get info of app located by index i in list
            ResolveInfo info = list.get(i);
            // get package name
            ComponentName cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
            component = cn.flattenToString();
            name = null;
            cacheValid = false;
            // get app data from cache
            if (!slow[0]) {
                AppData a = cache.get(component);
                if (a != null) {
                    name = a.name;
                    cacheValid = true;
                }
            }
            // if cache is not valid then set name from package manager
            if (!cacheValid) {
                name = info.activityInfo.loadLabel(pm).toString();
                if (name == null)
                    name = component;
                if (name.equals("Emerald Launcher")) {
                    continue;
                }
            }
            // add new appdata object to apps list
            apps.add(new AppData(component, name));
            // load icons
            if (icons) {
                // get icon file for app from cache
                File iconFile = MyCache.getIconFile(context, component);
                // if there is no icon for app in cache
                if (!cacheValid || !iconFile.exists()) {
                    // || ((Apps)context).iconPackChanged()) {
                    try {
                        writeIconTo(iconFile, pm.getResourcesForActivity(cn).getDrawable(pm.getPackageInfo(info.activityInfo.packageName, 0).applicationInfo.icon));
                    } catch (Exception e) {
                    }
                }
            }
        }
        // when apps are retrieved
        // save apps list in cache
        MyCache.write(context, CACHE_NAME, apps);
        // clean icons of deleted apps
        MyCache.cleanIcons(context, apps);
        publishProgress(list.size(), list.size());
    }
    return apps;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Intent(android.content.Intent) LauncherApps(android.content.pm.LauncherApps) ResolveInfo(android.content.pm.ResolveInfo) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) ComponentName(android.content.ComponentName) File(java.io.File)

Example 72 with LauncherActivityInfo

use of android.content.pm.LauncherActivityInfo in project emerald by HenriDellal.

the class AppsTaskLollipop method run.

@Override
public void run() {
    list = ((LauncherApps) (activity).getSystemService(Context.LAUNCHER_APPS_SERVICE)).getActivityList(null, Process.myUserHandle());
    for (i = 0; i < list.size(); i++) {
        Apps.handler.post(new Runnable() {

            @Override
            public void run() {
                Apps.progress.setIndeterminate(false);
                Apps.progress.setMax(list.size());
                Apps.progress.setProgress(i);
            }
        });
        LauncherActivityInfo info = list.get(i);
        ComponentName cn = info.getComponentName();
        String component = cn.flattenToString();
        if (!iconPackChanged && DatabaseHelper.hasApp(activity, component)) {
            continue;
        }
        String name = info.getLabel().toString();
        if (name == null) {
            name = component;
        } else if (name.equals(activity.getResources().getString(R.string.app_name))) {
            continue;
        }
        // load icons
        if (icons) {
            // get icon file for app from cache
            File iconFile = Cache.getIconFile(activity, component);
            // if there is no icon for app in cache
            if (!iconFile.exists()) {
                Apps.writeIconToFile(iconFile, info.getIcon(0), component);
            }
        }
        DatabaseHelper.insertApp(activity, component, name);
    }
    activity.options.edit().putString(Keys.PREV_APP_SHORTCUT, activity.options.getString(Keys.APP_SHORTCUT, "1")).commit();
    Apps.loadAppsHandler.sendEmptyMessage(0);
}
Also used : LauncherActivityInfo(android.content.pm.LauncherActivityInfo) ComponentName(android.content.ComponentName) File(java.io.File)

Example 73 with LauncherActivityInfo

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

the class TaskbarController method updateRecentApps.

@SuppressWarnings("Convert2streamapi")
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private void updateRecentApps(final boolean firstRefresh) {
    if (isScreenOff())
        return;
    updateSystemTray();
    SharedPreferences pref = U.getSharedPreferences(context);
    final PackageManager pm = context.getPackageManager();
    final List<AppEntry> entries = new ArrayList<>();
    List<LauncherActivityInfo> launcherAppCache = new ArrayList<>();
    int maxNumOfEntries = firstRefresh ? 0 : U.getMaxNumOfEntries(context);
    boolean fullLength = pref.getBoolean(PREF_FULL_LENGTH, true);
    PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
    List<AppEntry> pinnedApps = pba.getPinnedApps();
    List<AppEntry> blockedApps = pba.getBlockedApps();
    List<String> applicationIdsToRemove = new ArrayList<>();
    // Filter out anything on the pinned/blocked apps lists
    int realNumOfPinnedApps = filterRealPinnedApps(context, pinnedApps, entries, applicationIdsToRemove);
    if (blockedApps.size() > 0) {
        // noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (blockedApps) {
            for (AppEntry entry : blockedApps) {
                applicationIdsToRemove.add(entry.getPackageName());
            }
        }
    }
    // Get list of all recently used apps
    List<AppEntry> usageStatsList = realNumOfPinnedApps < maxNumOfEntries ? getAppEntries() : new ArrayList<>();
    if (usageStatsList.size() > 0 || realNumOfPinnedApps > 0 || fullLength) {
        if (realNumOfPinnedApps < maxNumOfEntries) {
            List<AppEntry> usageStatsList2 = new ArrayList<>();
            List<AppEntry> usageStatsList3 = new ArrayList<>();
            List<AppEntry> usageStatsList4 = new ArrayList<>();
            List<AppEntry> usageStatsList5 = new ArrayList<>();
            List<AppEntry> usageStatsList6;
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory(Intent.CATEGORY_HOME);
            ResolveInfo defaultLauncher = pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
            // Also filter out the current launcher, and Taskbar itself
            for (AppEntry packageInfo : usageStatsList) {
                if (hasLauncherIntent(packageInfo.getPackageName()) && !packageInfo.getPackageName().contains(BuildConfig.BASE_APPLICATION_ID) && !packageInfo.getPackageName().equals(defaultLauncher.activityInfo.packageName) && (!(U.launcherIsDefault(context) && pref.getBoolean(PREF_DESKTOP_MODE, false)) || !packageInfo.getPackageName().equals(pref.getString(PREF_HSL_ID, "null"))))
                    usageStatsList2.add(packageInfo);
            }
            // Filter out apps that don't fall within our current search interval
            for (AppEntry stats : usageStatsList2) {
                if (stats.getLastTimeUsed() > searchInterval || runningAppsOnly)
                    usageStatsList3.add(stats);
            }
            // Sort apps by either most recently used, or most time used
            if (!runningAppsOnly && sortOrder.contains("most_used")) {
                Collections.sort(usageStatsList3, (us1, us2) -> Long.compare(us2.getTotalTimeInForeground(), us1.getTotalTimeInForeground()));
            } else {
                Collections.sort(usageStatsList3, (us1, us2) -> Long.compare(us2.getLastTimeUsed(), us1.getLastTimeUsed()));
            }
            // Filter out any duplicate entries
            List<String> applicationIds = new ArrayList<>();
            for (AppEntry stats : usageStatsList3) {
                if (!applicationIds.contains(stats.getPackageName())) {
                    usageStatsList4.add(stats);
                    applicationIds.add(stats.getPackageName());
                }
            }
            // Filter out the currently running foreground app, if requested by the user
            filterForegroundApp(context, pref, searchInterval, applicationIdsToRemove);
            for (AppEntry stats : usageStatsList4) {
                if (!applicationIdsToRemove.contains(stats.getPackageName())) {
                    usageStatsList5.add(stats);
                }
            }
            // Truncate list to a maximum length
            if (usageStatsList5.size() > maxNumOfEntries)
                usageStatsList6 = usageStatsList5.subList(0, maxNumOfEntries);
            else
                usageStatsList6 = usageStatsList5;
            // Determine if we need to reverse the order
            if (needToReverseOrder(context, sortOrder)) {
                Collections.reverse(usageStatsList6);
            }
            // Generate the AppEntries for the recent apps list
            int number = usageStatsList6.size() == maxNumOfEntries ? usageStatsList6.size() - realNumOfPinnedApps : usageStatsList6.size();
            generateAppEntries(context, number, usageStatsList6, entries, launcherAppCache);
        }
        while (entries.size() > maxNumOfEntries) {
            try {
                entries.remove(entries.size() - 1);
                launcherAppCache.remove(launcherAppCache.size() - 1);
            } catch (ArrayIndexOutOfBoundsException ignored) {
            }
        }
        // Determine if we need to reverse the order again
        if (TaskbarPosition.isVertical(context)) {
            Collections.reverse(entries);
            Collections.reverse(launcherAppCache);
        }
        // Now that we've generated the list of apps,
        // we need to determine if we need to redraw the Taskbar or not
        boolean shouldRedrawTaskbar = firstRefresh;
        List<String> finalApplicationIds = new ArrayList<>();
        for (AppEntry entry : entries) {
            finalApplicationIds.add(entry.getPackageName());
        }
        int realNumOfSysTrayIcons = 0;
        for (Integer key : sysTrayIconStates.keySet()) {
            if (sysTrayIconStates.get(key))
                realNumOfSysTrayIcons++;
        }
        if (finalApplicationIds.size() != currentTaskbarIds.size() || numOfPinnedApps != realNumOfPinnedApps || numOfSysTrayIcons != realNumOfSysTrayIcons)
            shouldRedrawTaskbar = true;
        else {
            for (int i = 0; i < finalApplicationIds.size(); i++) {
                if (!finalApplicationIds.get(i).equals(currentTaskbarIds.get(i))) {
                    shouldRedrawTaskbar = true;
                    break;
                }
            }
        }
        if (shouldRedrawTaskbar) {
            currentTaskbarIds = finalApplicationIds;
            numOfPinnedApps = realNumOfPinnedApps;
            numOfSysTrayIcons = realNumOfSysTrayIcons;
            populateAppEntries(context, pm, entries, launcherAppCache);
            final int numOfEntries = Math.min(entries.size(), maxNumOfEntries);
            handler.post(() -> {
                if (numOfEntries > 0 || fullLength) {
                    ViewGroup.LayoutParams params = scrollView.getLayoutParams();
                    calculateScrollViewParams(context, pref, params, fullLength, numOfEntries);
                    scrollView.setLayoutParams(params);
                    for (Integer key : sysTrayIconStates.keySet()) {
                        sysTrayLayout.findViewById(key).setVisibility(sysTrayIconStates.get(key) ? View.VISIBLE : View.GONE);
                    }
                    taskbar.removeAllViews();
                    for (int i = 0; i < entries.size(); i++) {
                        taskbar.addView(getView(entries, i));
                    }
                    if (runningAppsOnly)
                        updateRunningAppIndicators(pinnedApps, usageStatsList, entries);
                    isShowingRecents = true;
                    if (shouldRefreshRecents && scrollView.getVisibility() != View.VISIBLE) {
                        if (firstRefresh)
                            scrollView.setVisibility(View.INVISIBLE);
                        else
                            scrollView.setVisibility(View.VISIBLE);
                    }
                    if (firstRefresh && scrollView.getVisibility() != View.VISIBLE) {
                        U.newHandler().post(() -> scrollTaskbar(scrollView, taskbar, TaskbarPosition.getTaskbarPosition(context), sortOrder, shouldRefreshRecents));
                    }
                } else {
                    isShowingRecents = false;
                    scrollView.setVisibility(View.GONE);
                }
            });
        } else if (runningAppsOnly)
            handler.post(() -> updateRunningAppIndicators(pinnedApps, usageStatsList, entries));
    } else if (firstRefresh || currentTaskbarIds.size() > 0) {
        currentTaskbarIds.clear();
        handler.post(() -> {
            isShowingRecents = false;
            scrollView.setVisibility(View.GONE);
        });
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) ViewGroup(android.view.ViewGroup) ArrayList(java.util.ArrayList) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) AppEntry(com.farmerbb.taskbar.util.AppEntry) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) PinnedBlockedApps(com.farmerbb.taskbar.util.PinnedBlockedApps) TargetApi(android.annotation.TargetApi)

Example 74 with LauncherActivityInfo

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

the class TaskbarController method drawStartButton.

@VisibleForTesting
void drawStartButton(Context context, ImageView startButton, SharedPreferences pref) {
    Drawable allAppsIcon = ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon);
    int padding = 0;
    switch(pref.getString(PREF_START_BUTTON_IMAGE, U.getDefaultStartButtonImage(context))) {
        case PREF_START_BUTTON_IMAGE_DEFAULT:
            startButton.setImageDrawable(allAppsIcon);
            padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
            break;
        case PREF_START_BUTTON_IMAGE_APP_LOGO:
            Drawable drawable;
            if (U.isBlissOs(context)) {
                drawable = ContextCompat.getDrawable(context, R.drawable.tb_bliss);
            } else if (U.isProjectSakura(context)) {
                drawable = ContextCompat.getDrawable(context, R.drawable.tb_sakura);
            } else {
                LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
                LauncherActivityInfo info = launcherApps.getActivityList(context.getPackageName(), Process.myUserHandle()).get(0);
                drawable = IconCache.getInstance(context).getIcon(context, context.getPackageManager(), info);
            }
            startButton.setImageDrawable(drawable);
            padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding_alt);
            break;
        case PREF_START_BUTTON_IMAGE_CUSTOM:
            U.applyCustomImage(context, "custom_image", startButton, allAppsIcon);
            padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
            break;
    }
    startButton.setPadding(padding, padding, padding, padding);
    startButton.setOnClickListener(ocl);
    startButton.setOnLongClickListener(view -> {
        openContextMenu();
        return true;
    });
    startButton.setOnGenericMotionListener((view, motionEvent) -> {
        if (motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
            openContextMenu();
        return false;
    });
}
Also used : Drawable(android.graphics.drawable.Drawable) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) LauncherApps(android.content.pm.LauncherApps) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 75 with LauncherActivityInfo

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

the class AppEntry method getIcon.

public Drawable getIcon(Context context) {
    if (icon == null) {
        if (iconByteArray != null)
            icon = new BitmapDrawable(context.getResources(), BitmapFactory.decodeByteArray(iconByteArray, 0, iconByteArray.length));
        else {
            Intent intent = new Intent();
            intent.setComponent(ComponentName.unflattenFromString(componentName));
            LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
            LauncherActivityInfo appInfo = launcherApps.resolveActivity(intent, userManager.getUserForSerialNumber(userId));
            icon = IconCache.getInstance(context).getIcon(context, appInfo);
        }
    }
    return icon;
}
Also used : UserManager(android.os.UserManager) LauncherActivityInfo(android.content.pm.LauncherActivityInfo) Intent(android.content.Intent) LauncherApps(android.content.pm.LauncherApps) BitmapDrawable(android.graphics.drawable.BitmapDrawable)

Aggregations

LauncherActivityInfo (android.content.pm.LauncherActivityInfo)122 LauncherApps (android.content.pm.LauncherApps)62 ArrayList (java.util.ArrayList)51 UserHandle (android.os.UserHandle)44 Intent (android.content.Intent)32 ComponentName (android.content.ComponentName)27 List (java.util.List)26 AppInfo (com.android.launcher3.model.data.AppInfo)23 ShortcutInfo (android.content.pm.ShortcutInfo)22 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)21 Context (android.content.Context)19 Point (android.graphics.Point)19 SessionInfo (android.content.pm.PackageInstaller.SessionInfo)16 SuppressLint (android.annotation.SuppressLint)14 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)13 PackageManager (android.content.pm.PackageManager)12 PackageUserKey (com.android.launcher3.util.PackageUserKey)11 UserManager (android.os.UserManager)10 CancellationException (java.util.concurrent.CancellationException)10 PackageInstaller (android.content.pm.PackageInstaller)9