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

Commit 99d40ed

Browse files
authored
Merge pull request #148 from TheAndroidMaster/develop
Version 3.8-beta4
2 parents 420e280 + 4cd8bc1 commit 99d40ed

File tree

4 files changed

+116
-17
lines changed

4 files changed

+116
-17
lines changed

app/src/main/java/com/james/status/Status.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.app.Application;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.content.SharedPreferences;
7+
import android.preference.PreferenceManager;
68
import android.support.annotation.Nullable;
79
import android.util.Log;
810
import android.widget.Toast;
@@ -12,6 +14,7 @@
1214

1315
import java.util.ArrayList;
1416
import java.util.List;
17+
import java.util.Map;
1518

1619
public class Status extends Application {
1720

@@ -26,6 +29,70 @@ public void onCreate() {
2629
onActivityResultListeners = new ArrayList<>();
2730
onColorPickedListeners = new ArrayList<>();
2831
onIconPreferenceChangedListeners = new ArrayList<>();
32+
33+
if ((int) PreferenceData.LAST_PREF_VERSION.getValue(this) == 0) {
34+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
35+
Map<String, ?> items = prefs.getAll();
36+
SharedPreferences.Editor editor = prefs.edit();
37+
for (String key : items.keySet()) {
38+
if (key.startsWith("COLOR/")) {
39+
String[] packages = key.substring("COLOR/".length()).split("/");
40+
if (packages.length == 2 && prefs.contains("COLOR/" + packages[0] + "/" + packages[1])) {
41+
try {
42+
PreferenceData.APP_COLOR.setValue(this, prefs.getInt(key, 0), packages[0] + "/" + packages[1]);
43+
} catch (Exception ignored) {
44+
}
45+
}
46+
} else if (key.startsWith("CACHE_COLOR/")) {
47+
String[] packages = key.substring("CACHE_COLOR/".length()).split("/");
48+
if (packages.length == 2 && prefs.contains("CACHE_COLOR/" + packages[0] + "/" + packages[1])) {
49+
try {
50+
PreferenceData.APP_COLOR_CACHE.setValue(this, prefs.getInt(key, 0), packages[0] + "/" + packages[1]);
51+
} catch (Exception ignored) {
52+
}
53+
}
54+
} else if (key.startsWith("CACHE_VERSION/")) {
55+
String[] packages = key.substring("CACHE_VERSION/".length()).split("/");
56+
if (packages.length == 2 && prefs.contains("CACHE_VERSION/" + packages[0] + "/" + packages[1])) {
57+
try {
58+
PreferenceData.APP_COLOR_CACHE_VERSION.setValue(this, prefs.getInt(key, 0), packages[0] + "/" + packages[1]);
59+
} catch (Exception ignored) {
60+
}
61+
}
62+
} else if (key.startsWith("FULLSCREEN/")) {
63+
String[] packages = key.substring("FULLSCREEN/".length()).split("/");
64+
if (packages.length == 2 && prefs.contains("FULLSCREEN/" + packages[0] + "/" + packages[1])) {
65+
try {
66+
if (prefs.getBoolean(key, false))
67+
PreferenceData.APP_FULLSCREEN.setValue(this, true, packages[0] + "/" + packages[1]);
68+
} catch (Exception ignored) {
69+
}
70+
}
71+
} else if (key.startsWith("IGNORE_AUTO_DETECT/")) {
72+
String[] packages = key.substring("IGNORE_AUTO_DETECT/".length()).split("/");
73+
if (packages.length == 2 && prefs.contains("IGNORE_AUTO_DETECT/" + packages[0] + "/" + packages[1])) {
74+
try {
75+
if (prefs.getBoolean(key, false))
76+
PreferenceData.APP_FULLSCREEN.setValue(this, true, packages[0] + "/" + packages[1]);
77+
} catch (Exception ignored) {
78+
}
79+
}
80+
} else if (key.startsWith("NOTIFICATIONS/")) {
81+
String[] packages = key.substring("NOTIFICATIONS/".length()).split("/");
82+
if (packages.length == 2 && prefs.contains("NOTIFICATIONS/" + packages[0] + "/" + packages[1])) {
83+
try {
84+
if (!prefs.getBoolean(key, true))
85+
PreferenceData.APP_NOTIFICATIONS.setValue(this, false, packages[0] + "/" + packages[1]);
86+
} catch (Exception ignored) {
87+
}
88+
}
89+
} else continue;
90+
91+
editor.remove(key);
92+
}
93+
editor.putInt("LAST_PREF_VERSION", PreferenceData.PREF_VERSION);
94+
editor.apply();
95+
}
2996
}
3097

3198
public void addListener(OnActivityResultListener listener) {

app/src/main/java/com/james/status/adapters/AppAdapter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public AppAdapter(Context context, List<AppPreferenceData> apps) {
3838
Collections.sort(apps, new Comparator<AppPreferenceData>() {
3939
@Override
4040
public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
41-
return lhs.getLabel(AppAdapter.this.context).compareToIgnoreCase(rhs.getLabel(AppAdapter.this.context));
41+
String label1 = lhs.getLabel(AppAdapter.this.context);
42+
String label2 = rhs.getLabel(AppAdapter.this.context);
43+
if (label1 != null && label2 != null)
44+
return label1.compareToIgnoreCase(label2);
45+
else return 0;
4246
}
4347
});
4448
}
@@ -112,7 +116,11 @@ public void filter(@Nullable final String string) {
112116
Collections.sort(apps, new Comparator<AppPreferenceData>() {
113117
@Override
114118
public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
115-
return lhs.getLabel(context).compareToIgnoreCase(rhs.getLabel(context));
119+
String label1 = lhs.getLabel(AppAdapter.this.context);
120+
String label2 = rhs.getLabel(AppAdapter.this.context);
121+
if (label1 != null && label2 != null)
122+
return label1.compareToIgnoreCase(label2);
123+
else return 0;
116124
}
117125
});
118126
} else {
@@ -121,9 +129,14 @@ public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
121129
public int compare(AppPreferenceData lhs, AppPreferenceData rhs) {
122130
int value = 0;
123131

124-
value += StringUtils.difference(lhs.getLabel(context).toLowerCase(), string).length();
132+
String label1 = lhs.getLabel(AppAdapter.this.context);
133+
String label2 = rhs.getLabel(AppAdapter.this.context);
134+
if (label1 != null && label2 != null) {
135+
value += StringUtils.difference(label1.toLowerCase(), string).length();
136+
value -= StringUtils.difference(label2.toLowerCase(), string).length();
137+
}
138+
125139
value += StringUtils.difference(lhs.getComponentName(), string).length();
126-
value -= StringUtils.difference(rhs.getLabel(context).toLowerCase(), string).length();
127140
value -= StringUtils.difference(rhs.getComponentName(), string).length();
128141

129142
return value;

app/src/main/java/com/james/status/data/PreferenceData.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222

2323
public enum PreferenceData {
24+
LAST_PREF_VERSION(0),
2425
STATUS_ENABLED(false),
2526
STATUS_NOTIFICATIONS_COMPAT(false),
2627
//STATUS_NOTIFICATIONS_HEADS_UP(false), TODO: #137
@@ -72,6 +73,8 @@ public enum PreferenceData {
7273
APP_FULLSCREEN_IGNORE("%1$s/APP_FULLSCREEN_IGNORE", false),
7374
APP_NOTIFICATIONS("%1$s/APP_NOTIFICATIONS", true);
7475

76+
public static final int PREF_VERSION = 1;
77+
7578
private String name;
7679
private Object defaultValue;
7780

app/src/main/java/com/james/status/fragments/AppPreferenceFragment.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.content.Context;
44
import android.content.DialogInterface;
55
import android.content.SharedPreferences;
6+
import android.content.pm.PackageManager;
7+
import android.os.Build;
68
import android.os.Bundle;
79
import android.preference.PreferenceManager;
810
import android.support.annotation.Nullable;
@@ -47,22 +49,36 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
4749
}
4850

4951
public void updateItems() {
50-
Map<String, AppPreferenceData> apps = new HashMap<>();
51-
Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(getContext()).getAll();
52-
for (String key : prefs.keySet()) {
53-
for (String pref : new String[]{"/APP_COLOR", "/APP_FULLSCREEN", "/APP_FULLSCREEN_IGNORE"}) {
54-
if (key.endsWith(pref)) {
55-
String component = key.substring(0, key.length() - pref.length()).split("/")[0];
56-
if (!apps.containsKey(component))
57-
apps.put(component, new AppPreferenceData(getContext(), component));
52+
Context context = getContext();
53+
if (context != null) {
54+
PackageManager manager = context.getPackageManager();
55+
if (manager != null) {
56+
Map<String, AppPreferenceData> apps = new HashMap<>();
57+
Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(getContext()).getAll();
58+
for (String key : prefs.keySet()) {
59+
for (String pref : new String[]{"/APP_COLOR", "/APP_FULLSCREEN", "/APP_FULLSCREEN_IGNORE"}) {
60+
if (key.endsWith(pref)) {
61+
String component = key.substring(0, key.length() - pref.length()).split("/")[0];
62+
try {
63+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
64+
manager.getPackageGids(component, 0);
65+
else manager.getPackageInfo(component, 0);
66+
} catch (PackageManager.NameNotFoundException e) {
67+
continue;
68+
}
69+
70+
if (!apps.containsKey(component))
71+
apps.put(component, new AppPreferenceData(getContext(), component));
72+
}
73+
}
5874
}
59-
}
60-
}
6175

62-
adapter = new AppAdapter(getContext(), new ArrayList<>(apps.values()));
63-
recycler.setAdapter(adapter);
76+
adapter = new AppAdapter(getContext(), new ArrayList<>(apps.values()));
77+
recycler.setAdapter(adapter);
6478

65-
emptyView.setVisibility(apps.size() > 0 ? View.GONE : View.VISIBLE);
79+
emptyView.setVisibility(apps.size() > 0 ? View.GONE : View.VISIBLE);
80+
}
81+
}
6682
}
6783

6884
public void reset() {

0 commit comments

Comments
 (0)