Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #148 from TheAndroidMaster/develop
Browse files Browse the repository at this point in the history
Version 3.8-beta4
  • Loading branch information
fennifith committed Sep 22, 2018
2 parents 420e280 + 4cd8bc1 commit 99d40ed
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 17 deletions.
67 changes: 67 additions & 0 deletions app/src/main/java/com/james/status/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
Expand All @@ -12,6 +14,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Status extends Application {

Expand All @@ -26,6 +29,70 @@ public void onCreate() {
onActivityResultListeners = new ArrayList<>();
onColorPickedListeners = new ArrayList<>();
onIconPreferenceChangedListeners = new ArrayList<>();

if ((int) PreferenceData.LAST_PREF_VERSION.getValue(this) == 0) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Map<String, ?> items = prefs.getAll();
SharedPreferences.Editor editor = prefs.edit();
for (String key : items.keySet()) {
if (key.startsWith("COLOR/")) {
String[] packages = key.substring("COLOR/".length()).split("/");
if (packages.length == 2 && prefs.contains("COLOR/" + packages[0] + "/" + packages[1])) {
try {
PreferenceData.APP_COLOR.setValue(this, prefs.getInt(key, 0), packages[0] + "/" + packages[1]);
} catch (Exception ignored) {
}
}
} else if (key.startsWith("CACHE_COLOR/")) {
String[] packages = key.substring("CACHE_COLOR/".length()).split("/");
if (packages.length == 2 && prefs.contains("CACHE_COLOR/" + packages[0] + "/" + packages[1])) {
try {
PreferenceData.APP_COLOR_CACHE.setValue(this, prefs.getInt(key, 0), packages[0] + "/" + packages[1]);
} catch (Exception ignored) {
}
}
} else if (key.startsWith("CACHE_VERSION/")) {
String[] packages = key.substring("CACHE_VERSION/".length()).split("/");
if (packages.length == 2 && prefs.contains("CACHE_VERSION/" + packages[0] + "/" + packages[1])) {
try {
PreferenceData.APP_COLOR_CACHE_VERSION.setValue(this, prefs.getInt(key, 0), packages[0] + "/" + packages[1]);
} catch (Exception ignored) {
}
}
} else if (key.startsWith("FULLSCREEN/")) {
String[] packages = key.substring("FULLSCREEN/".length()).split("/");
if (packages.length == 2 && prefs.contains("FULLSCREEN/" + packages[0] + "/" + packages[1])) {
try {
if (prefs.getBoolean(key, false))
PreferenceData.APP_FULLSCREEN.setValue(this, true, packages[0] + "/" + packages[1]);
} catch (Exception ignored) {
}
}
} else if (key.startsWith("IGNORE_AUTO_DETECT/")) {
String[] packages = key.substring("IGNORE_AUTO_DETECT/".length()).split("/");
if (packages.length == 2 && prefs.contains("IGNORE_AUTO_DETECT/" + packages[0] + "/" + packages[1])) {
try {
if (prefs.getBoolean(key, false))
PreferenceData.APP_FULLSCREEN.setValue(this, true, packages[0] + "/" + packages[1]);
} catch (Exception ignored) {
}
}
} else if (key.startsWith("NOTIFICATIONS/")) {
String[] packages = key.substring("NOTIFICATIONS/".length()).split("/");
if (packages.length == 2 && prefs.contains("NOTIFICATIONS/" + packages[0] + "/" + packages[1])) {
try {
if (!prefs.getBoolean(key, true))
PreferenceData.APP_NOTIFICATIONS.setValue(this, false, packages[0] + "/" + packages[1]);
} catch (Exception ignored) {
}
}
} else continue;

editor.remove(key);
}
editor.putInt("LAST_PREF_VERSION", PreferenceData.PREF_VERSION);
editor.apply();
}
}

public void addListener(OnActivityResultListener listener) {
Expand Down
21 changes: 17 additions & 4 deletions app/src/main/java/com/james/status/adapters/AppAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public AppAdapter(Context context, List<AppPreferenceData> apps) {
Collections.sort(apps, new Comparator<AppPreferenceData>() {
@Override
public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
return lhs.getLabel(AppAdapter.this.context).compareToIgnoreCase(rhs.getLabel(AppAdapter.this.context));
String label1 = lhs.getLabel(AppAdapter.this.context);
String label2 = rhs.getLabel(AppAdapter.this.context);
if (label1 != null && label2 != null)
return label1.compareToIgnoreCase(label2);
else return 0;
}
});
}
Expand Down Expand Up @@ -112,7 +116,11 @@ public void filter(@Nullable final String string) {
Collections.sort(apps, new Comparator<AppPreferenceData>() {
@Override
public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
return lhs.getLabel(context).compareToIgnoreCase(rhs.getLabel(context));
String label1 = lhs.getLabel(AppAdapter.this.context);
String label2 = rhs.getLabel(AppAdapter.this.context);
if (label1 != null && label2 != null)
return label1.compareToIgnoreCase(label2);
else return 0;
}
});
} else {
Expand All @@ -121,9 +129,14 @@ public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
int value = 0;

value += StringUtils.difference(lhs.getLabel(context).toLowerCase(), string).length();
String label1 = lhs.getLabel(AppAdapter.this.context);
String label2 = rhs.getLabel(AppAdapter.this.context);
if (label1 != null && label2 != null) {
value += StringUtils.difference(label1.toLowerCase(), string).length();
value -= StringUtils.difference(label2.toLowerCase(), string).length();
}

value += StringUtils.difference(lhs.getComponentName(), string).length();
value -= StringUtils.difference(rhs.getLabel(context).toLowerCase(), string).length();
value -= StringUtils.difference(rhs.getComponentName(), string).length();

return value;
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/james/status/data/PreferenceData.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;

public enum PreferenceData {
LAST_PREF_VERSION(0),
STATUS_ENABLED(false),
STATUS_NOTIFICATIONS_COMPAT(false),
//STATUS_NOTIFICATIONS_HEADS_UP(false), TODO: #137
Expand Down Expand Up @@ -72,6 +73,8 @@ public enum PreferenceData {
APP_FULLSCREEN_IGNORE("%1$s/APP_FULLSCREEN_IGNORE", false),
APP_NOTIFICATIONS("%1$s/APP_NOTIFICATIONS", true);

public static final int PREF_VERSION = 1;

private String name;
private Object defaultValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -47,22 +49,36 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
}

public void updateItems() {
Map<String, AppPreferenceData> apps = new HashMap<>();
Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(getContext()).getAll();
for (String key : prefs.keySet()) {
for (String pref : new String[]{"/APP_COLOR", "/APP_FULLSCREEN", "/APP_FULLSCREEN_IGNORE"}) {
if (key.endsWith(pref)) {
String component = key.substring(0, key.length() - pref.length()).split("/")[0];
if (!apps.containsKey(component))
apps.put(component, new AppPreferenceData(getContext(), component));
Context context = getContext();
if (context != null) {
PackageManager manager = context.getPackageManager();
if (manager != null) {
Map<String, AppPreferenceData> apps = new HashMap<>();
Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(getContext()).getAll();
for (String key : prefs.keySet()) {
for (String pref : new String[]{"/APP_COLOR", "/APP_FULLSCREEN", "/APP_FULLSCREEN_IGNORE"}) {
if (key.endsWith(pref)) {
String component = key.substring(0, key.length() - pref.length()).split("/")[0];
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
manager.getPackageGids(component, 0);
else manager.getPackageInfo(component, 0);
} catch (PackageManager.NameNotFoundException e) {
continue;
}

if (!apps.containsKey(component))
apps.put(component, new AppPreferenceData(getContext(), component));
}
}
}
}
}

adapter = new AppAdapter(getContext(), new ArrayList<>(apps.values()));
recycler.setAdapter(adapter);
adapter = new AppAdapter(getContext(), new ArrayList<>(apps.values()));
recycler.setAdapter(adapter);

emptyView.setVisibility(apps.size() > 0 ? View.GONE : View.VISIBLE);
emptyView.setVisibility(apps.size() > 0 ? View.GONE : View.VISIBLE);
}
}
}

public void reset() {
Expand Down

0 comments on commit 99d40ed

Please sign in to comment.