Home | History | Annotate | Download | only in batterystats
      1 /*
      2  * Copyright (C) 2017 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.server.cts.device.batterystats;
     18 
     19 import static com.android.server.cts.device.batterystats.BatteryStatsBgVsFgActions.ACTION_SLEEP_WHILE_TOP;
     20 import static com.android.server.cts.device.batterystats.BatteryStatsBgVsFgActions.ACTION_SYNC;
     21 import static com.android.server.cts.device.batterystats.BatteryStatsBgVsFgActions.KEY_ACTION;
     22 import static com.android.server.cts.device.batterystats.BatteryStatsBgVsFgActions.KEY_REQUEST_CODE;
     23 import static com.android.server.cts.device.batterystats.BatteryStatsBgVsFgActions.doAction;
     24 
     25 import android.app.Activity;
     26 import android.content.Intent;
     27 import android.os.Bundle;
     28 import android.util.Log;
     29 
     30 /** An activity (to be run as a foreground process) which performs one of a number of actions. */
     31 public class BatteryStatsForegroundActivity extends Activity {
     32     private static final String TAG = BatteryStatsForegroundActivity.class.getSimpleName();
     33 
     34     @Override
     35     public void onCreate(Bundle bundle) {
     36         super.onCreate(bundle);
     37 
     38         Intent intent = this.getIntent();
     39         if (intent == null) {
     40             Log.e(TAG, "Intent was null.");
     41             finish();
     42         }
     43 
     44         String action = intent.getStringExtra(KEY_ACTION);
     45         String requestCode = intent.getStringExtra(KEY_REQUEST_CODE);
     46         Log.i(TAG, "Starting " + action + " from foreground activity as request " + requestCode);
     47 
     48         // Check that app is in foreground; crash if it isn't.
     49         BatteryStatsBgVsFgActions.checkAppState(this, false, action, requestCode);
     50 
     51         doAction(this, action, requestCode);
     52 
     53         if (!isActionAsync(action)) {
     54             finish();
     55         }
     56     }
     57 
     58     private boolean isActionAsync(String action) {
     59         switch (action) {
     60             case ACTION_SYNC:
     61             case ACTION_SLEEP_WHILE_TOP:
     62                 return true;
     63         }
     64         return false;
     65     }
     66 }
     67