Home | History | Annotate | Download | only in jobscheduler
      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.example.android.jobscheduler;
     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.example.android.jobscheduler.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.sample_main);
     53         Resources res = getResources();
     54         defaultColor = res.getColor(R.color.none_received);
     55         startJobColor = res.getColor(R.color.start_received);
     56         stopJobColor = res.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         mServiceComponent = new ComponentName(this, TestJobService.class);
     69         // Start service and provide it a way to communicate with us.
     70         Intent startServiceIntent = new Intent(this, TestJobService.class);
     71         startServiceIntent.putExtra("messenger", new Messenger(mHandler));
     72         startService(startServiceIntent);
     73     }
     74     // UI fields.
     75     int defaultColor;
     76     int startJobColor;
     77     int stopJobColor;
     78 
     79     private TextView mShowStartView;
     80     private TextView mShowStopView;
     81     private TextView mParamsTextView;
     82     private EditText mDelayEditText;
     83     private EditText mDeadlineEditText;
     84     private RadioButton mWiFiConnectivityRadioButton;
     85     private RadioButton mAnyConnectivityRadioButton;
     86     private CheckBox mRequiresChargingCheckBox;
     87     private CheckBox mRequiresIdleCheckbox;
     88 
     89     ComponentName mServiceComponent;
     90     /** Service object to interact scheduled jobs. */
     91     TestJobService mTestService;
     92 
     93     private static int kJobId = 0;
     94 
     95     Handler mHandler = new Handler(/* default looper */) {
     96         @Override
     97         public void handleMessage(Message msg) {
     98             switch (msg.what) {
     99                 case MSG_UNCOLOUR_START:
    100                     mShowStartView.setBackgroundColor(defaultColor);
    101                     break;
    102                 case MSG_UNCOLOUR_STOP:
    103                     mShowStopView.setBackgroundColor(defaultColor);
    104                     break;
    105                 case MSG_SERVICE_OBJ:
    106                     mTestService = (TestJobService) msg.obj;
    107                     mTestService.setUiCallback(MainActivity.this);
    108             }
    109         }
    110     };
    111 
    112     private boolean ensureTestService() {
    113         if (mTestService == null) {
    114             Toast.makeText(MainActivity.this, "Service null, never got callback?",
    115                     Toast.LENGTH_SHORT).show();
    116             return false;
    117         }
    118         return true;
    119     }
    120 
    121     /**
    122      * UI onclick listener to schedule a job. What this job is is defined in
    123      * TestJobService#scheduleJob().
    124      */
    125     public void scheduleJob(View v) {
    126         if (!ensureTestService()) {
    127             return;
    128         }
    129 
    130         JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
    131 
    132         String delay = mDelayEditText.getText().toString();
    133         if (delay != null && !TextUtils.isEmpty(delay)) {
    134             builder.setMinimumLatency(Long.valueOf(delay) * 1000);
    135         }
    136         String deadline = mDeadlineEditText.getText().toString();
    137         if (deadline != null && !TextUtils.isEmpty(deadline)) {
    138             builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
    139         }
    140         boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
    141         boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
    142         if (requiresUnmetered) {
    143             builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
    144         } else if (requiresAnyConnectivity) {
    145             builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    146         }
    147         builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
    148         builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
    149 
    150         mTestService.scheduleJob(builder.build());
    151 
    152     }
    153 
    154     public void cancelAllJobs(View v) {
    155         JobScheduler tm =
    156                 (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
    157         tm.cancelAll();
    158     }
    159 
    160     /**
    161      * UI onclick listener to call jobFinished() in our service.
    162      */
    163     public void finishJob(View v) {
    164         if (!ensureTestService()) {
    165             return;
    166         }
    167         mTestService.callJobFinished();
    168         mParamsTextView.setText("");
    169     }
    170 
    171     /**
    172      * Receives callback from the service when a job has landed
    173      * on the app. Colours the UI and post a message to
    174      * uncolour it after a second.
    175      */
    176     public void onReceivedStartJob(JobParameters params) {
    177         mShowStartView.setBackgroundColor(startJobColor);
    178         Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START);
    179         mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second.
    180         mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras());
    181     }
    182 
    183     /**
    184      * Receives callback from the service when a job that
    185      * previously landed on the app must stop executing.
    186      * Colours the UI and post a message to uncolour it after a
    187      * second.
    188      */
    189     public void onReceivedStopJob() {
    190         mShowStopView.setBackgroundColor(stopJobColor);
    191         Message m = Message.obtain(mHandler, MSG_UNCOLOUR_STOP);
    192         mHandler.sendMessageDelayed(m, 2000L); // uncolour in 1 second.
    193         mParamsTextView.setText("");
    194     }
    195 }
    196