Home | History | Annotate | Download | only in jobSchedulerApp
      1 /*
      2  * Copyright 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.demo.jobSchedulerApp;
     18 
     19 import android.app.Activity;
     20 import android.app.job.JobInfo;
     21 import android.app.job.JobParameters;
     22 import android.app.job.JobScheduler;
     23 import android.content.ComponentName;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.content.res.Resources;
     27 import android.os.Bundle;
     28 import android.os.Handler;
     29 import android.os.Message;
     30 import android.os.Messenger;
     31 import android.text.TextUtils;
     32 import android.view.View;
     33 import android.widget.CheckBox;
     34 import android.widget.EditText;
     35 import android.widget.RadioButton;
     36 import android.widget.TextView;
     37 import android.widget.Toast;
     38 
     39 import com.android.demo.jobSchedulerApp.service.TestJobService;
     40 
     41 public class MainActivity extends Activity {
     42 
     43     private static final String TAG = "MainActivity";
     44 
     45     public static final int MSG_UNCOLOUR_START = 0;
     46     public static final int MSG_UNCOLOUR_STOP = 1;
     47     public static final int MSG_SERVICE_OBJ = 2;
     48 
     49     @Override
     50     public void onCreate(Bundle savedInstanceState) {
     51         super.onCreate(savedInstanceState);
     52         setContentView(R.layout.activity_main);
     53         Resources res = getResources();
     54         defaultColor = getColor(R.color.none_received);
     55         startJobColor = getColor(R.color.start_received);
     56         stopJobColor = getColor(R.color.stop_received);
     57 
     58         // Set up UI.
     59         mShowStartView = (TextView) findViewById(R.id.onstart_textview);
     60         mShowStopView = (TextView) findViewById(R.id.onstop_textview);
     61         mParamsTextView = (TextView) findViewById(R.id.task_params);
     62         mDelayEditText = (EditText) findViewById(R.id.delay_time);
     63         mDeadlineEditText = (EditText) findViewById(R.id.deadline_time);
     64         mWiFiConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_unmetered);
     65         mAnyConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_any);
     66         mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging);
     67         mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle);
     68         mIsPersistedCheckbox = (CheckBox) findViewById(R.id.checkbox_persisted);
     69 
     70         mServiceComponent = new ComponentName(this, TestJobService.class);
     71         // Start service and provide it a way to communicate with us.
     72         Intent startServiceIntent = new Intent(this, TestJobService.class);
     73         startServiceIntent.putExtra("messenger", new Messenger(mHandler));
     74         startService(startServiceIntent);
     75     }
     76     // UI fields.
     77     int defaultColor;
     78     int startJobColor;
     79     int stopJobColor;
     80 
     81     TextView mShowStartView;
     82     TextView mShowStopView;
     83     TextView mParamsTextView;
     84     EditText mDelayEditText;
     85     EditText mDeadlineEditText;
     86     RadioButton mWiFiConnectivityRadioButton;
     87     RadioButton mAnyConnectivityRadioButton;
     88     CheckBox mRequiresChargingCheckBox;
     89     CheckBox mRequiresIdleCheckbox;
     90     CheckBox mIsPersistedCheckbox;
     91 
     92     ComponentName mServiceComponent;
     93     /** Service object to interact scheduled jobs. */
     94     TestJobService mTestService;
     95 
     96     private static int kJobId = 0;
     97 
     98     Handler mHandler = new Handler(/* default looper */) {
     99         @Override
    100         public void handleMessage(Message msg) {
    101             switch (msg.what) {
    102                 case MSG_UNCOLOUR_START:
    103                     mShowStartView.setBackgroundColor(defaultColor);
    104                     break;
    105                 case MSG_UNCOLOUR_STOP:
    106                     mShowStopView.setBackgroundColor(defaultColor);
    107                     break;
    108                 case MSG_SERVICE_OBJ:
    109                     mTestService = (TestJobService) msg.obj;
    110                     mTestService.setUiCallback(MainActivity.this);
    111             }
    112         }
    113     };
    114 
    115     private boolean ensureTestService() {
    116         if (mTestService == null) {
    117             Toast.makeText(MainActivity.this, "Service null, never got callback?",
    118                     Toast.LENGTH_SHORT).show();
    119             return false;
    120         }
    121         return true;
    122     }
    123 
    124     /**
    125      * UI onclick listener to schedule a job. What this job is is defined in
    126      * TestJobService#scheduleJob()
    127      */
    128     public void scheduleJob(View v) {
    129         if (!ensureTestService()) {
    130             return;
    131         }
    132 
    133         JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
    134 
    135         String delay = mDelayEditText.getText().toString();
    136         if (delay != null && !TextUtils.isEmpty(delay)) {
    137             builder.setMinimumLatency(Long.valueOf(delay) * 1000);
    138         }
    139         String deadline = mDeadlineEditText.getText().toString();
    140         if (deadline != null && !TextUtils.isEmpty(deadline)) {
    141             builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
    142         }
    143         boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
    144         boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
    145         if (requiresUnmetered) {
    146             builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
    147         } else if (requiresAnyConnectivity) {
    148             builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    149         }
    150         builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
    151         builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
    152         builder.setPersisted(mIsPersistedCheckbox.isChecked());
    153         mTestService.scheduleJob(builder.build());
    154 
    155     }
    156 
    157     public void cancelAllJobs(View v) {
    158         JobScheduler tm =
    159                 (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
    160         tm.cancelAll();
    161     }
    162 
    163     /**
    164      * UI onclick listener to call jobFinished() in our service.
    165      */
    166     public void finishJob(View v) {
    167         if (!ensureTestService()) {
    168             return;
    169         }
    170         mTestService.callJobFinished();
    171         mParamsTextView.setText("");
    172     }
    173 
    174     public void onReceivedStartJob(JobParameters params) {
    175         mShowStartView.setBackgroundColor(startJobColor);
    176         Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START);
    177         mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second.
    178         mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras());
    179     }
    180 
    181     public void onReceivedStopJob() {
    182         mShowStopView.setBackgroundColor(stopJobColor);
    183         Message m = Message.obtain(mHandler, MSG_UNCOLOUR_STOP);
    184         mHandler.sendMessageDelayed(m, 2000L); // uncolour in 1 second.
    185         mParamsTextView.setText("");
    186     }
    187 }
    188