Home | History | Annotate | Download | only in notificationshowcase
      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 
     17 package com.android.example.notificationshowcase;
     18 
     19 import android.app.IntentService;
     20 import android.app.NotificationManager;
     21 import android.app.Service;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.os.Handler;
     25 import android.os.IBinder;
     26 import android.util.Log;
     27 
     28 public class ProgressService extends IntentService {
     29 
     30     private static final String TAG = "ProgressService";
     31 
     32     private static final String ACTION_PROGRESS = "progress";
     33 
     34     private Handler handler;
     35 
     36     public ProgressService() {
     37         super(TAG);
     38     }
     39     public ProgressService(String name) {
     40         super(name);
     41     }
     42 
     43     class UpdateRunnable implements Runnable {
     44 
     45         private final int mId;
     46         private final long mWhen;
     47         private int mProgress;
     48 
     49         UpdateRunnable(int id, long when, int progress) {
     50             mId = id;
     51             mWhen = when;
     52             mProgress = progress;
     53         }
     54 
     55         @Override
     56         public void run() {            NotificationManager noMa = (NotificationManager)
     57                 getSystemService(Context.NOTIFICATION_SERVICE);
     58                 Log.v(TAG, "id: " + mId + " when: " + mWhen + " progress: " + mProgress);
     59                 noMa.notify(NotificationService.NOTIFICATION_ID + mId,
     60                         NotificationService.makeUploadNotification(
     61                                 ProgressService.this, mProgress, mWhen));
     62                 mProgress += 2;
     63             if (mProgress <= 100) {
     64                 handler.postDelayed(this, 1000);
     65             }
     66         }
     67     }
     68 
     69     @Override
     70     public int onStartCommand(Intent intent, int flags, int startId) {
     71         handler = new Handler();
     72         return super.onStartCommand(intent, flags, startId);
     73     }
     74 
     75 
     76     @Override
     77     protected void onHandleIntent(Intent intent) {
     78         final int id = intent.getIntExtra("id", 0);
     79         final long when = intent.getLongExtra("when", 0L);
     80         int progress = intent.getIntExtra("progress", 0);
     81         handler.postDelayed(new UpdateRunnable(id, when, progress), 1000);
     82     }
     83 
     84     public static void startProgressUpdater(Context context, int id, long when, int progress) {
     85         Intent progressIntent = new Intent(context, ProgressService.class);
     86         progressIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     87         progressIntent.setAction(ACTION_PROGRESS);
     88         progressIntent.putExtra("id", id);
     89         progressIntent.putExtra("when", when);
     90         progressIntent.putExtra("progress", progress);
     91         context.startService(progressIntent);
     92     }
     93 }
     94