Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1f11f0e1 authored by Amit Kumar's avatar Amit Kumar
Browse files

Improves app suggestion flickering issue (Close #37)

parent 9eba774a
Loading
Loading
Loading
Loading
Loading
+27 −16
Original line number Original line Diff line number Diff line
@@ -102,6 +102,7 @@ import org.indin.blisslaunchero.framework.events.AppRemoveEvent;
import org.indin.blisslaunchero.framework.network.RetrofitService;
import org.indin.blisslaunchero.framework.network.RetrofitService;
import org.indin.blisslaunchero.framework.utils.AppUtils;
import org.indin.blisslaunchero.framework.utils.AppUtils;
import org.indin.blisslaunchero.framework.utils.GraphicsUtil;
import org.indin.blisslaunchero.framework.utils.GraphicsUtil;
import org.indin.blisslaunchero.framework.utils.ListUtil;
import org.json.JSONArray;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONObject;


@@ -117,14 +118,14 @@ import java.util.Set;
import java.util.UUID;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;


import cyanogenmod.weather.WeatherInfo;
import cyanogenmod.weather.util.WeatherUtils;
import io.reactivex.Observable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.schedulers.Schedulers;
import cyanogenmod.weather.WeatherInfo;
import cyanogenmod.weather.util.WeatherUtils;
import me.relex.circleindicator.CircleIndicator;
import me.relex.circleindicator.CircleIndicator;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;


@@ -179,6 +180,8 @@ public class LauncherActivity extends AppCompatActivity implements
    private View mWeatherSetupTextView;
    private View mWeatherSetupTextView;
    private boolean allAppsDisplayed;
    private boolean allAppsDisplayed;


    private List<UsageStats> mUsageStats;

    private BroadcastReceiver mWeatherReceiver = new BroadcastReceiver() {
    private BroadcastReceiver mWeatherReceiver = new BroadcastReceiver() {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
@@ -621,7 +624,6 @@ public class LauncherActivity extends AppCompatActivity implements
                    }
                    }
                }
                }
            }
            }
            addNewApp(packageName);
        }
        }
    }
    }


@@ -805,14 +807,19 @@ public class LauncherActivity extends AppCompatActivity implements
    private void refreshSuggestedApps() {
    private void refreshSuggestedApps() {
        AppUsageStats appUsageStats = new AppUsageStats(this);
        AppUsageStats appUsageStats = new AppUsageStats(this);
        List<UsageStats> usageStats = appUsageStats.getUsageStats();
        List<UsageStats> usageStats = appUsageStats.getUsageStats();

        if (usageStats.size() > 0) {
        if (usageStats.size() > 0) {
            if (suggestedAppsGridLayout != null && suggestedAppsGridLayout.getChildCount() > 0) {
            // Check if usage stats have been changed or not to avoid unnecessary flickering
            if (mUsageStats == null || mUsageStats.size() != usageStats.size()
                    || !ListUtil.areEqualLists(mUsageStats, usageStats)) {
                mUsageStats = usageStats;
                if (suggestedAppsGridLayout != null
                        && suggestedAppsGridLayout.getChildCount() > 0) {
                    suggestedAppsGridLayout.removeAllViews();
                    suggestedAppsGridLayout.removeAllViews();
                }
                }
                int i = 0;
                int i = 0;
            while (suggestedAppsGridLayout.getChildCount() < 4 && i < usageStats.size()) {
                while (suggestedAppsGridLayout.getChildCount() < 4 && i < mUsageStats.size()) {
                AppItem appItem = AppUtils.createAppItem(this, usageStats.get(i).getPackageName());
                    AppItem appItem = AppUtils.createAppItem(this,
                            mUsageStats.get(i).getPackageName());
                    if (appItem != null) {
                    if (appItem != null) {
                        BlissFrameLayout view = prepareSuggestedApp(appItem);
                        BlissFrameLayout view = prepareSuggestedApp(appItem);
                        addAppToGrid(suggestedAppsGridLayout, view);
                        addAppToGrid(suggestedAppsGridLayout, view);
@@ -821,6 +828,7 @@ public class LauncherActivity extends AppCompatActivity implements
                }
                }
            }
            }
        }
        }
    }


    /**
    /**
     * Populates the pages and the mDock for the first time.
     * Populates the pages and the mDock for the first time.
@@ -1030,6 +1038,7 @@ public class LauncherActivity extends AppCompatActivity implements
        AppUsageStats appUsageStats = new AppUsageStats(this);
        AppUsageStats appUsageStats = new AppUsageStats(this);
        List<UsageStats> usageStats = appUsageStats.getUsageStats();
        List<UsageStats> usageStats = appUsageStats.getUsageStats();
        if (usageStats.size() > 0) {
        if (usageStats.size() > 0) {
            mUsageStats = usageStats;
            int i = 0;
            int i = 0;
            while (suggestedAppsGridLayout.getChildCount() < 4 && i < usageStats.size()) {
            while (suggestedAppsGridLayout.getChildCount() < 4 && i < usageStats.size()) {
                AppItem appItem = AppUtils.createAppItem(this, usageStats.get(i).getPackageName());
                AppItem appItem = AppUtils.createAppItem(this, usageStats.get(i).getPackageName());
@@ -1103,7 +1112,8 @@ public class LauncherActivity extends AppCompatActivity implements
        mWeatherPanel = findViewById(R.id.weather_panel);
        mWeatherPanel = findViewById(R.id.weather_panel);
        updateWeatherPanel();
        updateWeatherPanel();


        if (org.indin.blisslaunchero.features.weather.WeatherUtils.isWeatherServiceAvailable(this)) {
        if (org.indin.blisslaunchero.features.weather.WeatherUtils.isWeatherServiceAvailable(
                this)) {
            startService(new Intent(this, WeatherSourceListenerService.class));
            startService(new Intent(this, WeatherSourceListenerService.class));
            startService(new Intent(this, DeviceStatusService.class));
            startService(new Intent(this, DeviceStatusService.class));
        }
        }
@@ -1118,7 +1128,8 @@ public class LauncherActivity extends AppCompatActivity implements
                        WeatherPreferences.LOCATION_PERMISSION_REQUEST_CODE);
                        WeatherPreferences.LOCATION_PERMISSION_REQUEST_CODE);
            } else {
            } else {
                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && Preferences.getEnableLocation(this)) {
                if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                        && Preferences.getEnableLocation(this)) {
                    showLocationEnableDialog();
                    showLocationEnableDialog();
                    Preferences.setEnableLocation(this);
                    Preferences.setEnableLocation(this);
                } else {
                } else {
+25 −13
Original line number Original line Diff line number Diff line
@@ -102,6 +102,7 @@ import org.indin.blisslaunchero.framework.events.AppRemoveEvent;
import org.indin.blisslaunchero.framework.network.RetrofitService;
import org.indin.blisslaunchero.framework.network.RetrofitService;
import org.indin.blisslaunchero.framework.utils.AppUtils;
import org.indin.blisslaunchero.framework.utils.AppUtils;
import org.indin.blisslaunchero.framework.utils.GraphicsUtil;
import org.indin.blisslaunchero.framework.utils.GraphicsUtil;
import org.indin.blisslaunchero.framework.utils.ListUtil;
import org.json.JSONArray;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONObject;


@@ -179,6 +180,8 @@ public class LauncherActivity extends AppCompatActivity implements
    private View mWeatherSetupTextView;
    private View mWeatherSetupTextView;
    private boolean allAppsDisplayed;
    private boolean allAppsDisplayed;


    private List<UsageStats> mUsageStats;

    private BroadcastReceiver mWeatherReceiver = new BroadcastReceiver() {
    private BroadcastReceiver mWeatherReceiver = new BroadcastReceiver() {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
@@ -804,14 +807,19 @@ public class LauncherActivity extends AppCompatActivity implements
    private void refreshSuggestedApps() {
    private void refreshSuggestedApps() {
        AppUsageStats appUsageStats = new AppUsageStats(this);
        AppUsageStats appUsageStats = new AppUsageStats(this);
        List<UsageStats> usageStats = appUsageStats.getUsageStats();
        List<UsageStats> usageStats = appUsageStats.getUsageStats();

        if (usageStats.size() > 0) {
        if (usageStats.size() > 0) {
            if (suggestedAppsGridLayout != null && suggestedAppsGridLayout.getChildCount() > 0) {
            // Check if usage stats have been changed or not to avoid unnecessary flickering
            if (mUsageStats == null || mUsageStats.size() != usageStats.size()
                    || !ListUtil.areEqualLists(mUsageStats, usageStats)) {
                mUsageStats = usageStats;
                if (suggestedAppsGridLayout != null
                        && suggestedAppsGridLayout.getChildCount() > 0) {
                    suggestedAppsGridLayout.removeAllViews();
                    suggestedAppsGridLayout.removeAllViews();
                }
                }
                int i = 0;
                int i = 0;
            while (suggestedAppsGridLayout.getChildCount() < 4 && i < usageStats.size()) {
                while (suggestedAppsGridLayout.getChildCount() < 4 && i < mUsageStats.size()) {
                AppItem appItem = AppUtils.createAppItem(this, usageStats.get(i).getPackageName());
                    AppItem appItem = AppUtils.createAppItem(this,
                            mUsageStats.get(i).getPackageName());
                    if (appItem != null) {
                    if (appItem != null) {
                        BlissFrameLayout view = prepareSuggestedApp(appItem);
                        BlissFrameLayout view = prepareSuggestedApp(appItem);
                        addAppToGrid(suggestedAppsGridLayout, view);
                        addAppToGrid(suggestedAppsGridLayout, view);
@@ -820,6 +828,7 @@ public class LauncherActivity extends AppCompatActivity implements
                }
                }
            }
            }
        }
        }
    }


    /**
    /**
     * Populates the pages and the mDock for the first time.
     * Populates the pages and the mDock for the first time.
@@ -1029,6 +1038,7 @@ public class LauncherActivity extends AppCompatActivity implements
        AppUsageStats appUsageStats = new AppUsageStats(this);
        AppUsageStats appUsageStats = new AppUsageStats(this);
        List<UsageStats> usageStats = appUsageStats.getUsageStats();
        List<UsageStats> usageStats = appUsageStats.getUsageStats();
        if (usageStats.size() > 0) {
        if (usageStats.size() > 0) {
            mUsageStats = usageStats;
            int i = 0;
            int i = 0;
            while (suggestedAppsGridLayout.getChildCount() < 4 && i < usageStats.size()) {
            while (suggestedAppsGridLayout.getChildCount() < 4 && i < usageStats.size()) {
                AppItem appItem = AppUtils.createAppItem(this, usageStats.get(i).getPackageName());
                AppItem appItem = AppUtils.createAppItem(this, usageStats.get(i).getPackageName());
@@ -1102,7 +1112,8 @@ public class LauncherActivity extends AppCompatActivity implements
        mWeatherPanel = findViewById(R.id.weather_panel);
        mWeatherPanel = findViewById(R.id.weather_panel);
        updateWeatherPanel();
        updateWeatherPanel();


        if (org.indin.blisslaunchero.features.weather.WeatherUtils.isWeatherServiceAvailable(this)) {
        if (org.indin.blisslaunchero.features.weather.WeatherUtils.isWeatherServiceAvailable(
                this)) {
            startService(new Intent(this, WeatherSourceListenerService.class));
            startService(new Intent(this, WeatherSourceListenerService.class));
            startService(new Intent(this, DeviceStatusService.class));
            startService(new Intent(this, DeviceStatusService.class));
        }
        }
@@ -1117,7 +1128,8 @@ public class LauncherActivity extends AppCompatActivity implements
                        WeatherPreferences.LOCATION_PERMISSION_REQUEST_CODE);
                        WeatherPreferences.LOCATION_PERMISSION_REQUEST_CODE);
            } else {
            } else {
                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && Preferences.getEnableLocation(this)) {
                if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                        && Preferences.getEnableLocation(this)) {
                    showLocationEnableDialog();
                    showLocationEnableDialog();
                    Preferences.setEnableLocation(this);
                    Preferences.setEnableLocation(this);
                } else {
                } else {
+10 −13
Original line number Original line Diff line number Diff line
@@ -2,16 +2,6 @@ package org.indin.blisslaunchero.features.usagestats;


import static android.app.usage.UsageStatsManager.INTERVAL_BEST;
import static android.app.usage.UsageStatsManager.INTERVAL_BEST;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.indin.blisslaunchero.R;

import android.app.usage.UsageStats;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Context;
@@ -20,6 +10,16 @@ import android.provider.Settings;
import android.util.Log;
import android.util.Log;
import android.widget.Toast;
import android.widget.Toast;


import org.indin.blisslaunchero.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AppUsageStats {
public class AppUsageStats {


    private final Context mContext;
    private final Context mContext;
@@ -58,7 +58,6 @@ public class AppUsageStats {
                    mContext.getString(R.string.explanation_access_to_appusage_is_not_enabled),
                    mContext.getString(R.string.explanation_access_to_appusage_is_not_enabled),
                    Toast.LENGTH_LONG).show();
                    Toast.LENGTH_LONG).show();
            mContext.startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
            mContext.startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
            Log.i(TAG, "getUsageStats: here");
        } else {
        } else {
            Set<Map.Entry<String, UsageStats>> set = aggregatedStats.entrySet();
            Set<Map.Entry<String, UsageStats>> set = aggregatedStats.entrySet();
            List<Map.Entry<String, UsageStats>> list = new ArrayList<>(set);
            List<Map.Entry<String, UsageStats>> list = new ArrayList<>(set);
@@ -69,8 +68,6 @@ public class AppUsageStats {
                usageStats.add(stringUsageStatsEntry.getValue());
                usageStats.add(stringUsageStatsEntry.getValue());
            }
            }
        }
        }
        Log.i(TAG, "getUsageStats: here2");

        return usageStats;
        return usageStats;
    }
    }
}
}
+33 −0
Original line number Original line Diff line number Diff line
@@ -15,9 +15,13 @@
 */
 */
package org.indin.blisslaunchero.framework.utils;
package org.indin.blisslaunchero.framework.utils;


import android.app.usage.UsageStats;

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.List;
import java.util.Set;


/**
/**
 * Created by Amit Kumar
 * Created by Amit Kumar
@@ -29,4 +33,33 @@ public class ListUtil {
    public static <T> List<T> asSafeList(T... tArr) {
    public static <T> List<T> asSafeList(T... tArr) {
        return (tArr == null || tArr.length == 0) ? new ArrayList() : Arrays.asList(tArr);
        return (tArr == null || tArr.length == 0) ? new ArrayList() : Arrays.asList(tArr);
    }
    }

    /**
     * To compare if two lists of {@link android.app.usage.UsageStats} contain same packages or not.
     *
     * @return true if both the lists contain the same packages otherwise false.
     */
    public static boolean areEqualLists(List<UsageStats> first, List<UsageStats> second) {
        if (first == null) {
            if (second == null) {
                return true;
            } else {
                return false;
            }
        }
        if (second == null) {
            return false;
        }

        Set<String> packages = new HashSet<>();
        for (UsageStats usageStats : first) {
            packages.add(usageStats.getPackageName());
        }

        for (UsageStats usageStats : second) {
            packages.remove(usageStats.getPackageName());
        }

        return packages.size() == 0;
    }
}
}