Skip to content

Commit

Permalink
Merge remote-tracking branch 'flow/notification-builder'
Browse files Browse the repository at this point in the history
This fixes #46, #47 and #48.

The code in here is fully backwards-compatible to older Android releases
using fallback to the old methods and reflection.
  • Loading branch information
ge0rg committed Jun 8, 2016
2 parents b6a3d55 + 1c6c803 commit d05a9a5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 18 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ buildscript {
apply plugin: 'com.android.library'

android {
compileSdkVersion 21
compileSdkVersion 23
buildToolsVersion "21.1.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 21
targetSdkVersion 23
}

sourceSets {
Expand Down
4 changes: 2 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ dependencies {
}

android {
compileSdkVersion 21
compileSdkVersion 23
buildToolsVersion "21.1.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 21
targetSdkVersion 23
}

sourceSets {
Expand Down
2 changes: 1 addition & 1 deletion example/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

android.library.reference.1=../
# Project target.
target=android-21
target=android-23
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

android.library=true
# Project target.
target=android-21
target=android-23
78 changes: 66 additions & 12 deletions src/de/duenndns/ssl/MemorizingTrustManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
import android.content.Intent;
import android.net.Uri;
import android.util.SparseArray;
import android.os.Build;
import android.os.Handler;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.cert.*;
import java.security.KeyStore;
import java.security.KeyStoreException;
Expand Down Expand Up @@ -566,19 +569,70 @@ private String hostNameMessage(X509Certificate cert, String hostname) {
return si.toString();
}

// We can use Notification.Builder once MTM's minSDK is >= 11
@SuppressWarnings("deprecation")
/**
* Reflectively call
* <code>Notification.setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)</code>
* since it was remove in Android API level 23.
*
* @param notification
* @param context
* @param mtmNotification
* @param certName
* @param call
*/
private static void setLatestEventInfoReflective(Notification notification,
Context context, CharSequence mtmNotification,
CharSequence certName, PendingIntent call) {
Method setLatestEventInfo;
try {
setLatestEventInfo = notification.getClass().getMethod(
"setLatestEventInfo", Context.class, CharSequence.class,
CharSequence.class, PendingIntent.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}

try {
setLatestEventInfo.invoke(notification, context, mtmNotification,
certName, call);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new IllegalStateException(e);
}
}

void startActivityNotification(Intent intent, int decisionId, String certName) {
Notification n = new Notification(android.R.drawable.ic_lock_lock,
master.getString(R.string.mtm_notification),
System.currentTimeMillis());
PendingIntent call = PendingIntent.getActivity(master, 0, intent, 0);
n.setLatestEventInfo(master.getApplicationContext(),
master.getString(R.string.mtm_notification),
certName, call);
n.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(NOTIFICATION_ID + decisionId, n);
Notification notification;
final PendingIntent call = PendingIntent.getActivity(master, 0, intent,
0);
final String mtmNotification = master.getString(R.string.mtm_notification);
final long currentMillis = System.currentTimeMillis();
final Context context = master.getApplicationContext();

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
@SuppressWarnings("deprecation")
// Use an extra identifier for the legacy build notification, so
// that we suppress the deprecation warning. We will latter assign
// this to the correct identifier.
Notification n = new Notification(android.R.drawable.ic_lock_lock,
mtmNotification,
currentMillis);
setLatestEventInfoReflective(n, context, mtmNotification, certName, call);
n.flags |= Notification.FLAG_AUTO_CANCEL;
notification = n;
} else {
notification = new Notification.Builder(master)
.setContentTitle(mtmNotification)
.setContentText(certName)
.setTicker(certName)
.setSmallIcon(android.R.drawable.ic_lock_lock)
.setWhen(currentMillis)
.setContentIntent(call)
.setAutoCancel(true)
.build();
}

notificationManager.notify(NOTIFICATION_ID + decisionId, notification);
}

/**
Expand Down

0 comments on commit d05a9a5

Please sign in to comment.