blob: 5aa0d4f35a21c3982c35883e9d96dd27e33c5984 [file] [log] [blame]
Matthew Williams13b02412014-06-06 19:05:44 -07001/*
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
17package com.android.demo.jobSchedulerApp;
18
19import android.app.Activity;
Christopher Tate7060b042014-06-09 19:50:00 -070020import android.app.job.JobInfo;
21import android.app.job.JobParameters;
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -070022import android.app.job.JobScheduler;
Matthew Williams13b02412014-06-06 19:05:44 -070023import android.content.ComponentName;
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -070024import android.content.Context;
Matthew Williams13b02412014-06-06 19:05:44 -070025import android.content.Intent;
26import android.content.res.Resources;
27import android.os.Bundle;
28import android.os.Handler;
29import android.os.Message;
30import android.os.Messenger;
31import android.text.TextUtils;
32import android.view.View;
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -070033import android.widget.CheckBox;
Matthew Williams13b02412014-06-06 19:05:44 -070034import android.widget.EditText;
35import android.widget.RadioButton;
36import android.widget.TextView;
37import android.widget.Toast;
38
39import com.android.demo.jobSchedulerApp.service.TestJobService;
40
41public 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 Viverette4a357cd2015-03-18 18:37:18 -070054 defaultColor = getColor(R.color.none_received);
55 startJobColor = getColor(R.color.start_received);
56 stopJobColor = getColor(R.color.stop_received);
Matthew Williams13b02412014-06-06 19:05:44 -070057
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 Williamsf7c4fddf2014-06-18 18:52:47 -070066 mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging);
67 mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle);
Matthew Williams9ae3dbe2014-08-21 13:47:47 -070068 mIsPersistedCheckbox = (CheckBox) findViewById(R.id.checkbox_persisted);
69
Matthew Williams13b02412014-06-06 19:05:44 -070070 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 Williamsf7c4fddf2014-06-18 18:52:47 -070088 CheckBox mRequiresChargingCheckBox;
89 CheckBox mRequiresIdleCheckbox;
Matthew Williams9ae3dbe2014-08-21 13:47:47 -070090 CheckBox mIsPersistedCheckbox;
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -070091
Matthew Williams13b02412014-06-06 19:05:44 -070092 ComponentName mServiceComponent;
Christopher Tate7060b042014-06-09 19:50:00 -070093 /** Service object to interact scheduled jobs. */
Matthew Williams13b02412014-06-06 19:05:44 -070094 TestJobService mTestService;
95
Christopher Tate7060b042014-06-09 19:50:00 -070096 private static int kJobId = 0;
Matthew Williams13b02412014-06-06 19:05:44 -070097
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 Tate7060b042014-06-09 19:50:00 -0700125 * UI onclick listener to schedule a job. What this job is is defined in
Matthew Williams13b02412014-06-06 19:05:44 -0700126 * TestJobService#scheduleJob()
127 */
128 public void scheduleJob(View v) {
129 if (!ensureTestService()) {
130 return;
131 }
132
Christopher Tate7060b042014-06-09 19:50:00 -0700133 JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
Matthew Williams13b02412014-06-06 19:05:44 -0700134
135 String delay = mDelayEditText.getText().toString();
136 if (delay != null && !TextUtils.isEmpty(delay)) {
Tobias Thierer28532d02016-04-21 14:52:10 +0100137 builder.setMinimumLatency(Long.parseLong(delay) * 1000);
Matthew Williams13b02412014-06-06 19:05:44 -0700138 }
139 String deadline = mDeadlineEditText.getText().toString();
140 if (deadline != null && !TextUtils.isEmpty(deadline)) {
Tobias Thierer28532d02016-04-21 14:52:10 +0100141 builder.setOverrideDeadline(Long.parseLong(deadline) * 1000);
Matthew Williams13b02412014-06-06 19:05:44 -0700142 }
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -0700143 boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
144 boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
Matthew Williams13b02412014-06-06 19:05:44 -0700145 if (requiresUnmetered) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700146 builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
Matthew Williams13b02412014-06-06 19:05:44 -0700147 } else if (requiresAnyConnectivity) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700148 builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
Matthew Williams13b02412014-06-06 19:05:44 -0700149 }
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -0700150 builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
151 builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
Matthew Williamsd1c06752014-08-22 14:15:28 -0700152 builder.setPersisted(mIsPersistedCheckbox.isChecked());
Matthew Williams13b02412014-06-06 19:05:44 -0700153 mTestService.scheduleJob(builder.build());
154
155 }
156
Matthew Williamsf7c4fddf2014-06-18 18:52:47 -0700157 public void cancelAllJobs(View v) {
158 JobScheduler tm =
159 (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
160 tm.cancelAll();
161 }
162
Matthew Williams13b02412014-06-06 19:05:44 -0700163 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700164 * UI onclick listener to call jobFinished() in our service.
Matthew Williams13b02412014-06-06 19:05:44 -0700165 */
166 public void finishJob(View v) {
167 if (!ensureTestService()) {
168 return;
169 }
Christopher Tate7060b042014-06-09 19:50:00 -0700170 mTestService.callJobFinished();
Matthew Williams13b02412014-06-06 19:05:44 -0700171 mParamsTextView.setText("");
172 }
173
Christopher Tate7060b042014-06-09 19:50:00 -0700174 public void onReceivedStartJob(JobParameters params) {
Matthew Williams13b02412014-06-06 19:05:44 -0700175 mShowStartView.setBackgroundColor(startJobColor);
176 Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START);
177 mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second.
Christopher Tate7060b042014-06-09 19:50:00 -0700178 mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras());
Matthew Williams13b02412014-06-06 19:05:44 -0700179 }
180
Christopher Tate7060b042014-06-09 19:50:00 -0700181 public void onReceivedStopJob() {
Matthew Williams13b02412014-06-06 19:05:44 -0700182 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}