diff options
| author | Matthew Williams <mjwilliams@google.com> | 2014-06-06 19:05:44 -0700 |
|---|---|---|
| committer | Matthew Williams <mjwilliams@google.com> | 2014-06-07 20:50:54 -0700 |
| commit | 13b0241248716b126a538457828a964e86109d58 (patch) | |
| tree | 4a12c8e46bc8d1318bb2c0521ae5df961e2632d7 /tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/MainActivity.java | |
| parent | 2f98a26bbf24fdc40ef53a653bb152c7f5a5d0dd (diff) | |
| download | frameworks_base-13b0241248716b126a538457828a964e86109d58.zip frameworks_base-13b0241248716b126a538457828a964e86109d58.tar.gz frameworks_base-13b0241248716b126a538457828a964e86109d58.tar.bz2 | |
Add test app for JobScheduler
Schedule either a delay/deadline task, or a task with
connectivity constraints
Change-Id: Ie7ea731d0f6673b680cef79f894cb609a61b795d
Diffstat (limited to 'tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/MainActivity.java')
| -rw-r--r-- | tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/MainActivity.java | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/MainActivity.java b/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/MainActivity.java new file mode 100644 index 0000000..393c594 --- /dev/null +++ b/tests/JobSchedulerTestApp/src/com/android/demo/jobSchedulerApp/MainActivity.java @@ -0,0 +1,169 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.demo.jobSchedulerApp; + +import android.app.Activity; +import android.app.task.Task; +import android.app.task.TaskParams; +import android.content.ComponentName; +import android.content.Intent; +import android.content.res.Resources; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; +import android.text.TextUtils; +import android.view.View; +import android.widget.EditText; +import android.widget.RadioButton; +import android.widget.TextView; +import android.widget.Toast; + +import com.android.demo.jobSchedulerApp.service.TestJobService; + +public class MainActivity extends Activity { + + private static final String TAG = "MainActivity"; + + public static final int MSG_UNCOLOUR_START = 0; + public static final int MSG_UNCOLOUR_STOP = 1; + public static final int MSG_SERVICE_OBJ = 2; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + Resources res = getResources(); + defaultColor = res.getColor(R.color.none_received); + startJobColor = res.getColor(R.color.start_received); + stopJobColor = res.getColor(R.color.stop_received); + + // Set up UI. + mShowStartView = (TextView) findViewById(R.id.onstart_textview); + mShowStopView = (TextView) findViewById(R.id.onstop_textview); + mParamsTextView = (TextView) findViewById(R.id.task_params); + mDelayEditText = (EditText) findViewById(R.id.delay_time); + mDeadlineEditText = (EditText) findViewById(R.id.deadline_time); + mWiFiConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_unmetered); + mAnyConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_any); + + mServiceComponent = new ComponentName(this, TestJobService.class); + // Start service and provide it a way to communicate with us. + Intent startServiceIntent = new Intent(this, TestJobService.class); + startServiceIntent.putExtra("messenger", new Messenger(mHandler)); + startService(startServiceIntent); + } + // UI fields. + int defaultColor; + int startJobColor; + int stopJobColor; + + TextView mShowStartView; + TextView mShowStopView; + TextView mParamsTextView; + EditText mDelayEditText; + EditText mDeadlineEditText; + RadioButton mWiFiConnectivityRadioButton; + RadioButton mAnyConnectivityRadioButton; + ComponentName mServiceComponent; + /** Service object to interact scheduled tasks. */ + TestJobService mTestService; + + private static int kTaskId = 0; + + Handler mHandler = new Handler(/* default looper */) { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_UNCOLOUR_START: + mShowStartView.setBackgroundColor(defaultColor); + break; + case MSG_UNCOLOUR_STOP: + mShowStopView.setBackgroundColor(defaultColor); + break; + case MSG_SERVICE_OBJ: + mTestService = (TestJobService) msg.obj; + mTestService.setUiCallback(MainActivity.this); + } + } + }; + + private boolean ensureTestService() { + if (mTestService == null) { + Toast.makeText(MainActivity.this, "Service null, never got callback?", + Toast.LENGTH_SHORT).show(); + return false; + } + return true; + } + + /** + * UI onclick listener to schedule a task. What this task is is defined in + * TestJobService#scheduleJob() + */ + public void scheduleJob(View v) { + if (!ensureTestService()) { + return; + } + + Task.Builder builder = new Task.Builder(kTaskId++, mServiceComponent); + + String delay = mDelayEditText.getText().toString(); + if (delay != null && !TextUtils.isEmpty(delay)) { + builder.setMinimumLatency(Long.valueOf(delay)); + } + String deadline = mDeadlineEditText.getText().toString(); + if (deadline != null && !TextUtils.isEmpty(deadline)) { + builder.setOverrideDeadline(Long.valueOf(deadline)); + } + boolean requiresUnmetered = mWiFiConnectivityRadioButton.isSelected(); + boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isSelected(); + if (requiresUnmetered) { + builder.setRequiredNetworkCapabilities(Task.NetworkType.UNMETERED); + } else if (requiresAnyConnectivity) { + builder.setRequiredNetworkCapabilities(Task.NetworkType.ANY); + } + + mTestService.scheduleJob(builder.build()); + + } + + /** + * UI onclick listener to call taskFinished() in our service. + */ + public void finishJob(View v) { + if (!ensureTestService()) { + return; + } + mTestService.callTaskFinished(); + mParamsTextView.setText(""); + } + + public void onReceivedStartTask(TaskParams params) { + mShowStartView.setBackgroundColor(startJobColor); + Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START); + mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second. + mParamsTextView.setText("Executing: " + params.getTaskId() + " " + params.getExtras()); + } + + public void onReceivedStopTask() { + mShowStopView.setBackgroundColor(stopJobColor); + Message m = Message.obtain(mHandler, MSG_UNCOLOUR_STOP); + mHandler.sendMessageDelayed(m, 2000L); // uncolour in 1 second. + mParamsTextView.setText(""); + } +} |
