Skip to content

Commit

Permalink
add app history, icon options...
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioCZ committed Jan 17, 2019
1 parent 22d8fa1 commit 81d0207
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 127 deletions.
56 changes: 28 additions & 28 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 43 additions & 8 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion 28
defaultConfig {
applicationId "com.gottlicher.notifrepeater"
minSdkVersion 28
minSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
Expand All @@ -26,4 +26,6 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "com.android.support:support-compat:28.0.0"
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
}
136 changes: 136 additions & 0 deletions app/src/main/java/com/gottlicher/notifrepeater/AppHistoryHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.gottlicher.notifrepeater;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Iterator;

public class AppHistoryHelper {
private final static String APPS_PREF_NAME = "APPS_PREF_NAME";

public static class AppInfo {
public String packageName;
public int imageRes;
public int ct;

AppInfo(String packageName, int imageRes, int ct) {
this.packageName = packageName;
this.imageRes = imageRes;
this.ct = ct;
}

public String getAppLauncherName (Context context) {
return getAppNameFromPackage(context, packageName);
}
}


private static Gson gson = new Gson();
public static void addNotificationOccurence (Context context, String appName)
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String appsJson = sharedPrefs.getString(APPS_PREF_NAME, "");

Type collType = new TypeToken<ArrayList<AppInfo>>(){}.getType();
ArrayList<AppInfo> appList = gson.fromJson(appsJson, collType);

if (appList == null) {
appList = new ArrayList<>();
}

boolean found = false;
for(AppInfo ai : appList) {
if(ai.packageName.equals(appName)) {
ai.ct++;
found = true;
}
}

if(!found) {
appList.add(new AppInfo(appName, R.drawable.ic_notifications_black_24dp, 1));
}

SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(APPS_PREF_NAME, gson.toJson(appList));
editor.apply();
}

public static ArrayList<AppInfo> getOccurences (Context context)
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String appsJson = sharedPrefs.getString(APPS_PREF_NAME, "");

Type collType = new TypeToken<ArrayList<AppInfo>>(){}.getType();
ArrayList<AppInfo> appList = gson.fromJson(appsJson, collType);
if (appList == null) {
appList = new ArrayList<>();
}
return appList;
}

public static int getIconFor (Context context, String appName)
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String appsJson = sharedPrefs.getString(APPS_PREF_NAME, "");

Type collType = new TypeToken<ArrayList<AppInfo>>(){}.getType();
ArrayList<AppInfo> appList = gson.fromJson(appsJson, collType);

for(AppInfo ai : appList) {
if(ai.packageName.equals(appName)) {
return ai.imageRes;
}
}
return R.drawable.ic_notifications_black_24dp;
}

public static void updateIcon (Context context, String packageName, int newIcon)
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String appsJson = sharedPrefs.getString(APPS_PREF_NAME, "");

Type collType = new TypeToken<ArrayList<AppInfo>>(){}.getType();
ArrayList<AppInfo> appList = gson.fromJson(appsJson, collType);

if (appList == null) {
appList = new ArrayList<>();
}

boolean found = false;
for(AppInfo ai : appList) {
if(ai.packageName.equals(packageName)) {
ai.imageRes = newIcon;
}
}

SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(APPS_PREF_NAME, gson.toJson(appList));
editor.apply();
}


public static String getAppNameFromPackage (Context context, String pkg)
{
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(pkg, 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
return (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
}
}
Loading

0 comments on commit 81d0207

Please sign in to comment.