Last active
August 29, 2015 14:25
-
-
Save Rafhack/cb96a6dd83b62723f84a to your computer and use it in GitHub Desktop.
NotificationEventHandler class helps to manage click events on views in a notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.PendingIntent; | |
import android.appwidget.AppWidgetProvider; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.support.annotation.NonNull; | |
import android.widget.RemoteViews; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* @author Rafael | |
* on 6/2/2015 at 4:47 PM. | |
*/ | |
public class NotificationEventHandler { | |
private static Map<Integer, EventInterface> eventsMap; | |
/* ====== CONSTRUCTORS ====== */ | |
private NotificationEventHandler(RemoteViews rvParent, int viewId, | |
EventInterface listener, Context context) { | |
EventProcessor thread = new EventProcessor(); | |
thread.execute(rvParent, viewId, listener, context); | |
} | |
/** | |
* @param rvParent The parent {@link RemoteViews} | |
* @param viewId The id of target view | |
* @param context The context | |
* @param listener The event listener | |
* | |
* @see br.net.meta_cortex.trinity.utils.NotificationEventHandler.EventInterface | |
* */ | |
public static NotificationEventHandler eventHandler( | |
RemoteViews rvParent, int viewId, Context context, EventInterface listener) { | |
return new NotificationEventHandler(rvParent, viewId, listener, context); | |
} | |
/* ====== CLASSES ====== */ | |
public static class NotificationEventHandlerReceiver extends AppWidgetProvider { | |
@Override | |
public void onReceive(@NonNull Context context, @NonNull Intent intent) { | |
new EventTrigger().execute(intent); | |
} | |
} | |
private static class EventTrigger extends AsyncTask<Intent, Void, EventInterface> { | |
@Override | |
protected EventInterface doInBackground(Intent... intents) { | |
return eventsMap.get(intents[0].getIntExtra("viewId", 0)); | |
} | |
@Override | |
protected void onPostExecute(EventInterface callback) { | |
super.onPostExecute(callback); | |
callback.onEvent(); | |
} | |
} | |
private class EventProcessor extends AsyncTask<Object, Void, Void> { | |
/** | |
* rvParent, viewId, listener, context | |
*/ | |
@Override | |
protected Void doInBackground(Object... params) { | |
Intent mIntent = new Intent("NotificationEventHandlerReceiver"); | |
mIntent.putExtra("viewId", (int) params[1]); | |
if (eventsMap == null) | |
eventsMap = new HashMap<>(); | |
eventsMap.put((int) params[1], (EventInterface) params[2]); | |
PendingIntent mPendingIntent = PendingIntent.getBroadcast((Context) params[3], | |
(int) params[1], mIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
((RemoteViews) params[0]).setOnClickPendingIntent((int) params[1], mPendingIntent); | |
return null; | |
} | |
} | |
/* ====== INTERFACES ====== */ | |
/** | |
* The method {@link EventInterface#onEvent()} will be called when the event is ready to be triggered | |
* */ | |
public interface EventInterface { | |
void onEvent(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment