Home | History | Annotate | Download | only in wakeuploop
      1 /*
      2  * Copyright (C) 2014 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 android.test.wakeuploop;
     18 
     19 import android.app.AlarmManager;
     20 import android.app.PendingIntent;
     21 import android.app.Service;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.os.Environment;
     25 import android.os.Handler;
     26 import android.os.IBinder;
     27 import android.os.Message;
     28 import android.os.Messenger;
     29 import android.os.SystemClock;
     30 import android.util.Log;
     31 
     32 import java.io.File;
     33 
     34 public class WakeLoopService extends Service {
     35 
     36     private static final String LOG_TAG = WakeLoopService.class.getSimpleName();
     37     static final String WAKEUP_INTERNAL = "WAKEUP_INTERVAL";
     38     static final String MAX_LOOP = "MAX_LOOP";
     39     static final String STOP_CALLBACK = "STOP_CALLBACK";
     40     static final String THIS_LOOP = "THIS_LOOP";
     41     static final int MSG_STOP_SERVICE = 0xd1ed1e;
     42 
     43     private final Handler mHandler = new Handler() {
     44         public void handleMessage(Message msg) {
     45             if (msg.what == MSG_STOP_SERVICE) {
     46                 stopSelf();
     47             } else {
     48                 super.handleMessage(msg);
     49             }
     50         };
     51     };
     52 
     53     @Override
     54     public IBinder onBind(Intent intent) {
     55         // no binding, just start via intent
     56         return null;
     57     }
     58 
     59     @Override
     60     public int onStartCommand(Intent intent, int flags, int startId) {
     61         // get wakeup interval from intent
     62         long wakeupInterval = intent.getLongExtra(WAKEUP_INTERNAL, 0);
     63         long maxLoop = intent.getLongExtra(MAX_LOOP, 0);
     64 
     65         if (wakeupInterval == 0) {
     66             // stop and error
     67             Log.e(LOG_TAG, "No wakeup interval specified, not starting the service");
     68             stopSelf();
     69             return START_NOT_STICKY;
     70         }
     71         FileUtil.get().writeDateToFile(new File(Environment.getExternalStorageDirectory(),
     72                 "wakeup-loop-start.txt"));
     73         Log.d(LOG_TAG, String.format("WakeLoop: STARTED interval = %d, total loop = %d",
     74                 wakeupInterval, maxLoop));
     75         // calculate when device should be waken up
     76         long atTime = SystemClock.elapsedRealtime() + wakeupInterval;
     77         AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
     78         Intent wakupIntent = new Intent(WakeUpCall.WAKEUP_CALL)
     79             .putExtra(WAKEUP_INTERNAL, wakeupInterval)
     80             .putExtra(MAX_LOOP, maxLoop)
     81             .putExtra(THIS_LOOP, 0L)
     82             .putExtra(STOP_CALLBACK, new Messenger(mHandler));
     83         PendingIntent pi = PendingIntent.getBroadcast(this, 0, wakupIntent,
     84                 PendingIntent.FLAG_UPDATE_CURRENT);
     85         // set alarm, which will be delivered in form of the wakeupIntent
     86         am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
     87         return START_NOT_STICKY;
     88     }
     89 
     90     @Override
     91     public void onDestroy() {
     92         Log.d(LOG_TAG, "WakeLoop: STOPPED");
     93         // cancel alarms first
     94         Intent intent = new Intent(WakeUpCall.WAKEUP_CALL)
     95             .putExtra(WakeUpCall.CANCEL, "true");
     96         sendBroadcast(intent);
     97     }
     98 }
     99