Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 20 | import android.app.job.JobInfo; |
| 21 | import android.app.job.JobParameters; |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 22 | import android.app.job.JobScheduler; |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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; |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 33 | import android.widget.CheckBox; |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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(); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 54 | defaultColor = getColor(R.color.none_received); |
| 55 | startJobColor = getColor(R.color.start_received); |
| 56 | stopJobColor = getColor(R.color.stop_received); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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); |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 66 | mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging); |
| 67 | mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 68 | mIsPersistedCheckbox = (CheckBox) findViewById(R.id.checkbox_persisted); |
| 69 | |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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; |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 88 | CheckBox mRequiresChargingCheckBox; |
| 89 | CheckBox mRequiresIdleCheckbox; |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 90 | CheckBox mIsPersistedCheckbox; |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 91 | |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 92 | ComponentName mServiceComponent; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 93 | /** Service object to interact scheduled jobs. */ |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 94 | TestJobService mTestService; |
| 95 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 96 | private static int kJobId = 0; |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 125 | * UI onclick listener to schedule a job. What this job is is defined in |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 126 | * TestJobService#scheduleJob() |
| 127 | */ |
| 128 | public void scheduleJob(View v) { |
| 129 | if (!ensureTestService()) { |
| 130 | return; |
| 131 | } |
| 132 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 133 | JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 134 | |
| 135 | String delay = mDelayEditText.getText().toString(); |
| 136 | if (delay != null && !TextUtils.isEmpty(delay)) { |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 137 | builder.setMinimumLatency(Long.parseLong(delay) * 1000); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 138 | } |
| 139 | String deadline = mDeadlineEditText.getText().toString(); |
| 140 | if (deadline != null && !TextUtils.isEmpty(deadline)) { |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 141 | builder.setOverrideDeadline(Long.parseLong(deadline) * 1000); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 142 | } |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 143 | boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked(); |
| 144 | boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked(); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 145 | if (requiresUnmetered) { |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 146 | builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 147 | } else if (requiresAnyConnectivity) { |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 148 | builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 149 | } |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 150 | builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked()); |
| 151 | builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked()); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 152 | builder.setPersisted(mIsPersistedCheckbox.isChecked()); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 153 | mTestService.scheduleJob(builder.build()); |
| 154 | |
| 155 | } |
| 156 | |
Matthew Williams | f7c4fddf | 2014-06-18 18:52:47 -0700 | [diff] [blame] | 157 | public void cancelAllJobs(View v) { |
| 158 | JobScheduler tm = |
| 159 | (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); |
| 160 | tm.cancelAll(); |
| 161 | } |
| 162 | |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 163 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 164 | * UI onclick listener to call jobFinished() in our service. |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 165 | */ |
| 166 | public void finishJob(View v) { |
| 167 | if (!ensureTestService()) { |
| 168 | return; |
| 169 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 170 | mTestService.callJobFinished(); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 171 | mParamsTextView.setText(""); |
| 172 | } |
| 173 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 174 | public void onReceivedStartJob(JobParameters params) { |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 175 | mShowStartView.setBackgroundColor(startJobColor); |
| 176 | Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START); |
| 177 | mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 178 | mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras()); |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 181 | public void onReceivedStopJob() { |
Matthew Williams | 13b0241 | 2014-06-06 19:05:44 -0700 | [diff] [blame] | 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 | } |