118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu/*
218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Copyright (C) 2014 The Android Open Source Project
318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu *
418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Licensed under the Apache License, Version 2.0 (the "License");
518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * you may not use this file except in compliance with the License.
618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * You may obtain a copy of the License at
718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu *
818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu *      http://www.apache.org/licenses/LICENSE-2.0
918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu *
1018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * Unless required by applicable law or agreed to in writing, software
1118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * distributed under the License is distributed on an "AS IS" BASIS,
1218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * See the License for the specific language governing permissions and
1418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * limitations under the License.
1518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */
1618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
1718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhupackage android.test.wakeuploop;
1818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
1918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.app.AlarmManager;
2018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.app.PendingIntent;
2118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.content.BroadcastReceiver;
2218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.content.Context;
2318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.content.Intent;
2418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.Environment;
2518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.Message;
2618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.Messenger;
2718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.PowerManager;
2818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.PowerManager.WakeLock;
2918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.RemoteException;
3018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.os.SystemClock;
3118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport android.util.Log;
3218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
3318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhuimport java.io.File;
3418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
3518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu/**
3618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu * The receiver for the alarm we set
3718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu *
3818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu */
3918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhupublic class WakeUpCall extends BroadcastReceiver {
4018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    private static final String LOG_TAG = WakeUpCall.class.getSimpleName();
4118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    static final String WAKEUP_CALL = "android.test.wakeuploop.WAKEUP";
4218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    static final String CANCEL = "CANCEL";
4318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
4418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    @Override
4518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    public void onReceive(Context context, Intent intent) {
4618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
4718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        boolean cancel = intent.hasExtra(CANCEL);
4818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        if (!cancel) {
4918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            long maxLoop = intent.getLongExtra(WakeLoopService.MAX_LOOP, 0);
5018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            long wakeupInterval = intent.getLongExtra(WakeLoopService.WAKEUP_INTERNAL, 0);
5118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            long thisLoop = intent.getLongExtra(WakeLoopService.THIS_LOOP, -1);
5218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            Log.d(LOG_TAG, String.format("incoming: interval = %d, max loop = %d, this loop = %d",
5318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                    wakeupInterval, maxLoop, thisLoop));
5418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            if (thisLoop == -1) {
5518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                Log.e(LOG_TAG, "no valid loop count received, trying to stop service");
5618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                stopService(intent);
5718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                return;
5818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            }
5918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            if (wakeupInterval == 0) {
6018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                Log.e(LOG_TAG, "no valid wakeup interval received, trying to stop service");
6118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                stopService(intent);
6218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                return;
6318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            }
6418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            thisLoop++;
6518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            Log.d(LOG_TAG, String.format("WakeLoop - iteration %d of %d", thisLoop, maxLoop));
6618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            if (thisLoop == maxLoop) {
6718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                // when maxLoop is 0, we loop forever, so not checking that case
6818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                // here
6918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                Log.d(LOG_TAG, "reached max loop count, stopping service");
7018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                stopService(intent);
7118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                return;
7218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            }
7318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            screenOn(context);
7418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            FileUtil.get().writeDateToFile(
7518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                    new File(Environment.getExternalStorageDirectory(), "wakeup-loop.txt"));
7618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            // calculate when device should be waken up
7718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            long atTime = SystemClock.elapsedRealtime() + wakeupInterval;
7818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            intent.putExtra(WakeLoopService.THIS_LOOP, thisLoop);
7918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
8018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                    PendingIntent.FLAG_UPDATE_CURRENT);
8118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            // set alarm, which will be delivered in form of the wakeupIntent
8218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
8318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        } else {
8418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            // cancel alarms
8518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            Log.d(LOG_TAG, "cancelling future alarms on request");
8618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            am.cancel(PendingIntent.getBroadcast(context, 0, intent, 0));
8718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        }
8818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
8918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
9018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    private void stopService(Intent i) {
9118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        Messenger msgr = i.getParcelableExtra(WakeLoopService.STOP_CALLBACK);
9218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        if (msgr == null) {
9318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            Log.e(LOG_TAG, "no stop service callback found, cannot stop");
9418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        } else {
9518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            Message msg = new Message();
9618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            msg.what = WakeLoopService.MSG_STOP_SERVICE;
9718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            try {
9818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                msgr.send(msg);
9918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            } catch (RemoteException e) {
10018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                Log.e(LOG_TAG, "ignored remoted exception while attempting to stop service", e);
10118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu            }
10218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        }
10318b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
10418b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu
10518b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    private void screenOn(Context context) {
10618b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
10718b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        @SuppressWarnings("deprecation")
10818b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK |
10918b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu                PowerManager.ACQUIRE_CAUSES_WAKEUP, LOG_TAG);
11018b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu        wl.acquire(500);
11118b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu    }
11218b892c723e812a7e111f102d2b0c0782b116bb6Guang Zhu}
113