ProgressService.java revision ac5e6eee61916fc3fa1a25ed4ef7ae92a1c9323c
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.example.notificationshowcase; 18 19import android.app.IntentService; 20import android.app.NotificationManager; 21import android.app.PendingIntent; 22import android.content.Context; 23import android.content.Intent; 24import android.os.Handler; 25import android.util.Log; 26 27public class ProgressService extends IntentService { 28 29 private static final String TAG = "ProgressService"; 30 31 private static final String ACTION_PROGRESS = "progress"; 32 private static final String ACTION_SILENT = "silent"; 33 34 private static ProgressService.UpdateRunnable mUpdateRunnable; 35 36 private Handler mHandler; 37 38 public ProgressService() { 39 super(TAG); 40 } 41 public ProgressService(String name) { 42 super(name); 43 } 44 45 class UpdateRunnable implements Runnable { 46 47 private final int mId; 48 private final long mWhen; 49 private int mProgress; 50 51 UpdateRunnable(int id, long when, int progress) { 52 mId = NotificationService.NOTIFICATION_ID + id; 53 mWhen = when; 54 mProgress = progress; 55 } 56 57 @Override 58 public void run() { 59 NotificationManager noMa = (NotificationManager) 60 getSystemService(Context.NOTIFICATION_SERVICE); 61 if (mUpdateRunnable != null) { 62 Log.v(TAG, "id: " + mId + " when: " + mWhen + " progress: " + mProgress); 63 noMa.notify(mId, NotificationService.makeUploadNotification( 64 ProgressService.this, mProgress, mWhen)); 65 mProgress += 2; 66 if (mProgress <= 100) { 67 mHandler.postDelayed(mUpdateRunnable, 100); 68 } 69 } else { 70 noMa.cancel(mId); 71 Log.d(TAG, "mUpdateRunnable is null "); 72 } 73 } 74 } 75 76 @Override 77 public int onStartCommand(Intent intent, int flags, int startId) { 78 mHandler = new Handler(); 79 return super.onStartCommand(intent, flags, startId); 80 } 81 82 83 @Override 84 protected void onHandleIntent(Intent intent) { 85 Log.d(TAG, "onHandleIntent " + intent.getAction()); 86 if (ACTION_PROGRESS.equals(intent.getAction())) { 87 final int id = intent.getIntExtra("id", 0); 88 final long when = intent.getLongExtra("when", 0L); 89 int progress = intent.getIntExtra("progress", 0); 90 mUpdateRunnable = new UpdateRunnable(id, when, progress); 91 mHandler.postDelayed(mUpdateRunnable, 1000); 92 } else if (ACTION_SILENT.equals(intent.getAction())) { 93 Log.d(TAG, "cancelling "); 94 if (mUpdateRunnable != null) { 95 mUpdateRunnable = null; 96 } 97 } 98 } 99 100 public static void startProgressUpdater(Context context, int id, long when, int progress) { 101 Intent progressIntent = new Intent(context, ProgressService.class); 102 progressIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 103 progressIntent.setAction(ACTION_PROGRESS); 104 progressIntent.putExtra("id", id); 105 progressIntent.putExtra("when", when); 106 progressIntent.putExtra("progress", progress); 107 context.startService(progressIntent); 108 } 109 110 public static PendingIntent getSilencePendingIntent(Context context) { 111 Log.d(TAG, "getSilencePendingIntent "); 112 Intent silenceIntent = new Intent(context, ProgressService.class); 113 silenceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 114 silenceIntent.setAction(ACTION_SILENT); 115 PendingIntent pi = PendingIntent.getService( 116 context, 0, silenceIntent, PendingIntent.FLAG_UPDATE_CURRENT); 117 return pi; 118 } 119} 120