Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Commit

Permalink
Notification channels - targeting Android 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hexadec committed Aug 15, 2018
1 parent f51c53c commit 0fc749e
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 50 deletions.
2 changes: 1 addition & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-main-apk-res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaPrecompile" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifest-checker" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/prebuild" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard-rules" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/resources" />
Expand Down
19 changes: 13 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ android {
defaultConfig {
applicationId "hu.kfg.naplo"
minSdkVersion 16
targetSdkVersion 25
versionCode 10104
versionName "1.1.beta4"
targetSdkVersion 26
versionCode 10200
versionName "1.2.beta0"
}
buildTypes {
release {
minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

debug {
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile 'com.evernote:android-job:1.2.6'
implementation 'com.evernote:android-job:1.2.6'
}
6 changes: 6 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

-keep class hu.hexadecimal.** { *; }
-dontwarn hu.kfg.**
-keepattributes Exceptions, Signature, InnerClasses
-dontobfuscate
#-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!code/allocation/variable
6 changes: 3 additions & 3 deletions app/src/main/java/hu/hexadec/killerwhale/OrcaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public OrcaManager(Context context, String[] excludeVendor) throws FileNotFoundE
}
} catch (IOException e) {
throw new FileNotFoundException();
} catch (NumberFormatException e) {
throw new FileNotFoundException("Invalid file!");
} catch (ArrayIndexOutOfBoundsException e) {
} catch (NumberFormatException|ArrayIndexOutOfBoundsException e) {
throw new FileNotFoundException("Invalid file!");
} catch (Exception e) {
throw new FileNotFoundException(e.getMessage());
}
}

Expand Down
85 changes: 77 additions & 8 deletions app/src/main/java/hu/kfg/naplo/ChangeListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package hu.kfg.naplo;

import android.content.*;
import android.graphics.Color;
import android.opengl.Visibility;
import android.preference.*;

import android.net.*;
Expand Down Expand Up @@ -42,6 +44,10 @@ public class ChangeListener {
static final int DONE = 0;
static final int DONE_NO_CHANGE = 1;

static final String CHANNEL_STANDINS = "standins";
static final String CHANNEL_GRADES = "grades";
static final String CHANNEL_NIGHT = "night";

public static void onRunJob(final Context context, final Intent intent) {
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final String mode = pref.getString("notification_mode", MODE_FALSE);
Expand Down Expand Up @@ -109,7 +115,7 @@ public void run() {
URL url = new URL(kfgserver);
urlConnection = (HttpURLConnection) url.openConnection();
String userAgentPrefix = System.getProperty("http.agent", "Mozilla/5.0 ");
userAgentPrefix = userAgentPrefix.substring(0, userAgentPrefix.indexOf("(")>0 ? userAgentPrefix.indexOf("(") : userAgentPrefix.length());
userAgentPrefix = userAgentPrefix.substring(0, userAgentPrefix.indexOf("(") > 0 ? userAgentPrefix.indexOf("(") : userAgentPrefix.length());
urlConnection.setRequestProperty("User-Agent", userAgentPrefix + "(Android " + Build.VERSION.RELEASE + "; Karinthy Naplo v" + BuildConfig.VERSION_NAME + ")");
urlConnection.setInstanceFollowRedirects(true);
} catch (IOException e) {
Expand Down Expand Up @@ -153,6 +159,11 @@ public void run() {
});
}
return GYIA_ERROR;
} else {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null)
notificationManager.cancel(1);
}
BufferedReader rd = new BufferedReader
(new InputStreamReader(urlConnection.getInputStream(), "ISO-8859-2"));
Expand Down Expand Up @@ -531,6 +542,12 @@ public void run() {
}

private static void notifyIfChanged(int[] state, Context context, String url, String subjects) {
try {
setUpNotificationChannels(context);
} catch (Exception e) {
Log.e(TAG, "Error while creating channels!");
e.printStackTrace();
}
Intent intent = new Intent(context, TableViewActivity.class);
Intent eintent = new Intent(Intent.ACTION_VIEW);
eintent.setData(Uri.parse(url));
Expand All @@ -555,8 +572,13 @@ private static void notifyIfChanged(int[] state, Context context, String url, St
}
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
PendingIntent epIntent = PendingIntent.getActivity(context, 0, eintent, 0);
Notification.Builder n = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.app_name))
Notification.Builder n;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
n = new Notification.Builder(context, nightmode || state[0] == 1 ? CHANNEL_NIGHT : CHANNEL_GRADES);
} else {
n = new Notification.Builder(context);
}
n.setContentTitle(context.getString(R.string.app_name))
.setContentText(state[0] == 0 ? context.getString(R.string.new_grade) : context.getString(R.string.gyia_expired_not))
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= 21) {
Expand All @@ -578,7 +600,6 @@ private static void notifyIfChanged(int[] state, Context context, String url, St
n.addAction(android.R.drawable.ic_input_get, context.getString(R.string.grade_table), pIntent);
Notification notification = new Notification.BigTextStyle(n)
.bigText(((state[0] == 0 ? context.getString(R.string.new_grade) + "\n" : "") + subjects + oldtext)).build();
// notification.number = numberoflessons;
notificationManager.notify(state[0], notification);
pref.edit().putString("oldtext", subjects.length() > 100 ? subjects.substring(0, subjects.indexOf(",", 90)) + "…" : subjects).commit();
}
Expand Down Expand Up @@ -606,17 +627,65 @@ private static String SHA512(byte[] data) throws Exception {
return sb.toString();
}

static void setUpNotificationChannels(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

//Setup standins channel
NotificationChannel notificationChannel =
new NotificationChannel(CHANNEL_STANDINS, context.getString(R.string.standins), NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(0xff00FF88);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{0, 60, 100, 70, 100, 60});
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);

//Setup grades channel
notificationChannel =
new NotificationChannel(CHANNEL_GRADES, context.getString(R.string.grades), NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(0xff00FF88);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{0, 60, 100, 70, 100, 60});
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(notificationChannel);

//Setup nightmode channel
notificationChannel =
new NotificationChannel(CHANNEL_NIGHT, context.getString(R.string.night_notifications), NotificationManager.IMPORTANCE_LOW);
notificationChannel.enableLights(false);
notificationChannel.enableVibration(false);
notificationChannel.setVibrationPattern(new long[]{0, 60, 100, 70, 100, 60});
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(notificationChannel);
}
}

private static void notifyIfStandinsChanged(int[] state, Context context, String classs, String subjects, int numberoflessons) {
try {
setUpNotificationChannels(context);
} catch (Exception e) {
Log.e(TAG, "Error while creating channels!");
e.printStackTrace();
}
int time = Integer.valueOf(new SimpleDateFormat("HHmm", Locale.US).format(new Date()));
boolean nightmode = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("nightmode", false) && (time > NIGHTMODE_START || time < NIGHTMODE_STOP);
Intent eintent = new Intent(Intent.ACTION_VIEW);
eintent.setData(Uri.parse("https://apps.karinthy.hu/helyettesites"));
PendingIntent epIntent = PendingIntent.getActivity(context, 0, eintent, 0);
Notification.Builder n = new Notification.Builder(context)
.setContentTitle(context.getString(R.string.kfg_standins))
Notification.Builder n;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
n = new Notification.Builder(context, state[0] != 3 && !nightmode ? CHANNEL_STANDINS : CHANNEL_NIGHT);
} else {
n = new Notification.Builder(context);
}
n.setContentTitle(context.getString(R.string.kfg_standins))
.setContentText(state[0] == 0 ? context.getString(R.string.new_substitution2) + " (" + classs + ")" + subjects : context.getString(R.string.no_new_substitution2) + " (" + classs + ")")
.setSmallIcon(R.drawable.ic_standins)
.setAutoCancel(true);
int time = Integer.valueOf(new SimpleDateFormat("HHmm", Locale.US).format(new Date()));
boolean nightmode = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("nightmode", false) && (time > NIGHTMODE_START || time < NIGHTMODE_STOP);
if (state[1] == 1 && state[0] != 3 && !nightmode) {
n.setVibrate(new long[]{0, 60, 100, 70, 100, 60});
}
Expand Down
37 changes: 37 additions & 0 deletions app/src/main/java/hu/kfg/naplo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ protected void onCreate(Bundle savedInstanceState) {
final Preference interval = findPreference("auto_check_interval");
final Preference vibrate = findPreference("vibrate");
final Preference flash = findPreference("flash");
final Preference ngrades = findPreference("not_grades");
final Preference nstandins = findPreference("not_standins");
final Preference open_in_browser = findPreference("open_in_browser");
final Preference nightmode = findPreference("nightmode");
final Preference ignore = findPreference("ignore_lessons");
Expand All @@ -49,6 +51,36 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

PreferenceCategory cat = (PreferenceCategory) findPreference("main");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
cat.removePreference(vibrate);
cat.removePreference(flash);
ChangeListener.setUpNotificationChannels(this);
ngrades.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, ChangeListener.CHANNEL_GRADES);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(intent);
return true;
}
});
nstandins.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, ChangeListener.CHANNEL_STANDINS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(intent);
return true;
}
});
} else {
cat.removePreference(ngrades);
cat.removePreference(nstandins);
}

final InputFilter teacherFilter = new InputFilter() {

@Override
Expand Down Expand Up @@ -86,11 +118,14 @@ public CharSequence filter(CharSequence source, int start, int end, Spanned dest
clas.setEnabled(false);
url.setEnabled(false);
ignore.setEnabled(false);
ngrades.setEnabled(false);
nstandins.setEnabled(false);
JobManager.instance().cancelAll();
break;
case ChangeListener.MODE_NAPLO:
clas.setEnabled(false);
ignore.setEnabled(false);
nstandins.setEnabled(false);
CheckerJob.runJobImmediately();
break;
case ChangeListener.MODE_TEACHER:
Expand All @@ -99,10 +134,12 @@ public CharSequence filter(CharSequence source, int start, int end, Spanned dest
clas.setTitle(R.string.teacher_name);
url.setEnabled(false);
ignore.setEnabled(false);
ngrades.setEnabled(false);
CheckerJob.scheduleJob();
break;
case ChangeListener.MODE_STANDINS:
url.setEnabled(false);
ngrades.setEnabled(false);
default:
clas.getEditText().setFilters(new InputFilter[]{classFilter});
clas.setSummary(R.string.yourclass_sum);
Expand Down
Loading

0 comments on commit 0fc749e

Please sign in to comment.