Skip to content

Commit

Permalink
notification phase 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mootoh committed Feb 25, 2012
1 parent 54ead3e commit 7e0da30
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 21 deletions.
8 changes: 8 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -51,6 +51,14 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<service
android:name=".TouchService"
android:exported="false" >
<intent-filter>
<action android:name="net.mootoh.toggltouch.START_TASK" />
</intent-filter>
</service>
</application>

</manifest>
Binary file added res/drawable-hdpi/ic_notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-ldpi/ic_notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/net/mootoh/toggltouch/SettingActivity.java
Expand Up @@ -61,6 +61,10 @@ private void setupSyncButton() {
Button syncButton = (Button)findViewById(R.id.syncButton);
syncButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TouchService.ACTION_START);
intent.putExtra(TouchService.TASK_DESCRIPTION, "aa");
startService(intent);

syncTasks();
}
});
Expand Down
35 changes: 14 additions & 21 deletions src/net/mootoh/toggltouch/Tag.java
Expand Up @@ -7,13 +7,16 @@
import org.json.JSONException;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public final class Tag {
public String id;
Expand Down Expand Up @@ -204,11 +207,7 @@ public void onSucceeded(Integer result) {
}
self.assignTask(touchedTask, activity);
Tag.setCurrent(activity, self);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, touchedTask.getDescription() + " start.", Toast.LENGTH_SHORT).show();
}
});
self.postNotification(touchedTask, true, activity);
}

public void onFailed(Exception e) {
Expand All @@ -225,11 +224,7 @@ public void onFailed(Exception e) {
public void onSucceeded(Integer result) {
Log.d("StopTimeEntryDelegate", "onSucceeded: " + result);
Tag.resetCurrent(activity);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, touchedTask.getDescription() + " end.", Toast.LENGTH_SHORT).show();
}
});
postNotification(touchedTask, false, activity);
}

public void onFailed(Exception e) {
Expand All @@ -248,11 +243,7 @@ public void onFailed(Exception e) {
public void onSucceeded(Integer result) {
Log.d("StopTimeEntryDelegate", "onSucceeded: " + result);
Tag.resetCurrent(activity);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, currentTask.getDescription() + " end.", Toast.LENGTH_SHORT).show();
}
});
postNotification(currentTask, false, activity);

try {
api.startTimeEntry(touchedTask, new ApiResponseDelegate<Integer>() {
Expand All @@ -266,11 +257,7 @@ public void onSucceeded(Integer result) {
}
self.assignTask(touchedTask, activity);
Tag.setCurrent(activity, self);
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, touchedTask.getDescription() + " start.", Toast.LENGTH_SHORT).show();
}
});
postNotification(touchedTask, false, activity);
}

public void onFailed(Exception e) {
Expand All @@ -292,4 +279,10 @@ public void onFailed(Exception e) {
}
}
}

private void postNotification(Task task, boolean started, Activity activity) {
Intent intent = new Intent(started ? TouchService.ACTION_START : TouchService.ACTION_STOP);
intent.putExtra(TouchService.TASK_DESCRIPTION, task.getDescription());
activity.startService(intent);
}
}
66 changes: 66 additions & 0 deletions src/net/mootoh/toggltouch/TouchService.java
@@ -0,0 +1,66 @@
package net.mootoh.toggltouch;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public final class TouchService extends Service {
protected static final String ACTION_START = "net.mootoh.toggltouch.START_TASK";
protected static final String ACTION_STOP = "net.mootoh.toggltouch.STOP_TASK";
protected static final String TASK_DESCRIPTION = "TASK_DESCRIPTION";
private static final int TASK_NOTIFICATION_ID = 1;

NotificationManager notificationManager;

@Override
public void onCreate() {
Log.d(getClass().getSimpleName(), "Service created");
super.onCreate();

notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(getClass().getSimpleName(), "Service started");

String action = intent.getAction();
String taskDescription = intent.getExtras().getString(TASK_DESCRIPTION);

String msg = taskDescription + (action.equals(ACTION_START) ? " started." : " stopped.");
Notification.Builder nbuilder = new Notification.Builder(this)
.setTicker(msg)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("TogglTouch")
.setContentText("Yey!");

Notification notification = nbuilder.getNotification();
/*
Context context = getApplicationContext();
Intent notificationIntent = new Intent(context, SettingActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
*/
notificationManager.notify(TASK_NOTIFICATION_ID, notification);

return START_STICKY;
}

@Override
public void onDestroy() {
Log.d(getClass().getSimpleName(), "destroying");
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}

0 comments on commit 7e0da30

Please sign in to comment.