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