Skip to content

Commit

Permalink
Simplify NotificationEventPlugin && Correct its compatibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed Feb 19, 2018
1 parent cf729cc commit e45da4a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public class NotificationEventData extends TypedEventData {
private static final String K_TITLE = "title";
private static final String K_CONTENT = "content";

NotificationSelection selection;
String app;
String title;
String content;

{
default_type = EventType.after;
Expand All @@ -52,8 +54,10 @@ public class NotificationEventData extends TypedEventData {

public NotificationEventData() {}

public NotificationEventData(NotificationSelection selection) {
this.selection = selection;
NotificationEventData(String app, String title, String content) {
this.app = app;
this.title = title;
this.content = content;
}

NotificationEventData(@NonNull String data, @NonNull C.Format format, int version) throws IllegalStorageDataException {
Expand All @@ -63,9 +67,13 @@ public NotificationEventData(NotificationSelection selection) {
@SuppressWarnings({"SimplifiableIfStatement", "RedundantIfStatement"})
@Override
public boolean isValid() {
if (selection == null)
return false;
return true;
if (app != null)
return true;
if (title != null)
return true;
if (content != null)
return true;
return false;
}

@SuppressWarnings({"SimplifiableIfStatement", "RedundantIfStatement"})
Expand All @@ -79,11 +87,11 @@ public boolean equals(Object obj) {
return false;
if (!Utils.eEquals(this, (EventData) obj))
return false;
if (!Utils.nullableEqual(selection.app, ((NotificationEventData) obj).selection.app))
if (!Utils.nullableEqual(app, ((NotificationEventData) obj).app))
return false;
if (!Utils.nullableEqual(selection.title, ((NotificationEventData) obj).selection.title))
if (!Utils.nullableEqual(title, ((NotificationEventData) obj).title))
return false;
if (!Utils.nullableEqual(selection.content, ((NotificationEventData) obj).selection.content))
if (!Utils.nullableEqual(content, ((NotificationEventData) obj).content))
return false;
return true;
}
Expand All @@ -100,14 +108,13 @@ public void serialize(XmlSerializer serializer) throws IOException {

@Override
public void parse(@NonNull String data, @NonNull C.Format format, int version) throws IllegalStorageDataException {
selection = new NotificationSelection();
switch (format) {
default:
try {
JSONObject jsonObject = new JSONObject(data);
selection.app = jsonObject.optString(K_APP, null);
selection.title = jsonObject.optString(K_TITLE, null);
selection.content = jsonObject.optString(K_CONTENT, null);
app = jsonObject.optString(K_APP, null);
title = jsonObject.optString(K_TITLE, null);
content = jsonObject.optString(K_CONTENT, null);
} catch (JSONException e) {
e.printStackTrace();
}
Expand All @@ -122,9 +129,12 @@ public String serialize(@NonNull C.Format format) {
default:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put(K_APP, selection.app);
jsonObject.put(K_TITLE, selection.title);
jsonObject.put(K_CONTENT, selection.content);
if (!Utils.isBlank(app))
jsonObject.put(K_APP, app);
if (!Utils.isBlank(title))
jsonObject.put(K_TITLE, title);
if (!Utils.isBlank(content))
jsonObject.put(K_CONTENT, content);
} catch (JSONException e) {
throw new IllegalStateException(e);
}
Expand All @@ -140,9 +150,9 @@ public int describeContents() {

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(selection.app);
dest.writeString(selection.title);
dest.writeString(selection.content);
dest.writeString(app);
dest.writeString(title);
dest.writeString(content);
}

public static final Creator<NotificationEventData> CREATOR
Expand All @@ -157,9 +167,8 @@ public NotificationEventData[] newArray(int size) {
};

private NotificationEventData(Parcel in) {
selection = new NotificationSelection();
selection.app = in.readString();
selection.title = in.readString();
selection.content = in.readString();
app = in.readString();
title = in.readString();
content = in.readString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ public NotificationEventData emptyData() {
@NonNull
@Override
public NotificationEventData dummyData() {
NotificationEventData dummyData = new NotificationEventData();
NotificationSelection notificationSelection = new NotificationSelection();
notificationSelection.app = "example.app";
notificationSelection.title = "title example";
notificationSelection.content = "content example";
dummyData.selection = notificationSelection;
return dummyData;
String app = "example.app";
String title = "title example";
String content = "content example";
return new NotificationEventData(app, title, content);
}

@ValidData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ static boolean isRunning() {
return is_running;
}

private static boolean match(StatusBarNotification sbn, NotificationSelection selection) {
Logger.d("app: <%s> <%s>", selection.app, sbn.getPackageName());
if (selection.app != null) {
if (!selection.app.equals(sbn.getPackageName()))
private static boolean match(StatusBarNotification sbn, String t_app, String t_title, String t_content) {
Logger.d("app: <%s> <%s>", t_app, sbn.getPackageName());
if (t_app != null) {
if (!t_app.equals(sbn.getPackageName()))
return false;
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString(Notification.EXTRA_TITLE);
String contentText = extras.getString(Notification.EXTRA_TEXT);
if (selection.title != null) {
if (title == null || !title.contains(selection.title))
if (t_title != null) {
if (title == null || !title.contains(t_title))
return false;
}
if (selection.content != null) {
if (contentText == null || !contentText.contains(selection.content))
if (t_content != null) {
if (contentText == null || !contentText.contains(t_content))
return false;
}
return true;
Expand All @@ -103,7 +103,7 @@ private static boolean match(StatusBarNotification sbn, NotificationSelection se
public void onNotificationPosted(StatusBarNotification sbn) {
for (CompoundData compoundData : dataList) {
NotificationEventData eventData = compoundData.notificationEventData;
if (match(sbn, eventData.selection)) {
if (match(sbn, eventData.app, eventData.title, eventData.content)) {
try {
compoundData.positive.send();
} catch (PendingIntent.CanceledException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public int name() {

@Override
public boolean isCompatible(@NonNull final Context context) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
//TODO support Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

@Override
protected void _fill(@ValidData @NonNull NotificationEventData data) {
NotificationSelection notificationSelection = data.selection;
editText_app.setText(notificationSelection.app);
editText_title.setText(notificationSelection.title);
editText_content.setText(notificationSelection.content);
editText_app.setText(data.app);
editText_title.setText(data.title);
editText_content.setText(data.content);
}

@ValidData
@NonNull
@Override
public NotificationEventData getData() throws InvalidDataInputException {
NotificationSelection notificationSelection = new NotificationSelection();
notificationSelection.app = textOf(editText_app);
notificationSelection.title = textOf(editText_title);
notificationSelection.content = textOf(editText_content);
return new NotificationEventData(notificationSelection);
String app = textOf(editText_app);
String title = textOf(editText_title);
String content = textOf(editText_content);
return new NotificationEventData(app, title, content);
}

@Nullable
Expand Down

This file was deleted.

0 comments on commit e45da4a

Please sign in to comment.