113b0241248716b126a538457828a964e86109d58Matthew Williams/*
213b0241248716b126a538457828a964e86109d58Matthew Williams * Copyright 2013 The Android Open Source Project
313b0241248716b126a538457828a964e86109d58Matthew Williams *
413b0241248716b126a538457828a964e86109d58Matthew Williams * Licensed under the Apache License, Version 2.0 (the "License");
513b0241248716b126a538457828a964e86109d58Matthew Williams * you may not use this file except in compliance with the License.
613b0241248716b126a538457828a964e86109d58Matthew Williams * You may obtain a copy of the License at
713b0241248716b126a538457828a964e86109d58Matthew Williams *
813b0241248716b126a538457828a964e86109d58Matthew Williams *      http://www.apache.org/licenses/LICENSE-2.0
913b0241248716b126a538457828a964e86109d58Matthew Williams *
1013b0241248716b126a538457828a964e86109d58Matthew Williams * Unless required by applicable law or agreed to in writing, software
1113b0241248716b126a538457828a964e86109d58Matthew Williams * distributed under the License is distributed on an "AS IS" BASIS,
1213b0241248716b126a538457828a964e86109d58Matthew Williams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313b0241248716b126a538457828a964e86109d58Matthew Williams * See the License for the specific language governing permissions and
1413b0241248716b126a538457828a964e86109d58Matthew Williams * limitations under the License.
1513b0241248716b126a538457828a964e86109d58Matthew Williams */
1613b0241248716b126a538457828a964e86109d58Matthew Williams
1713b0241248716b126a538457828a964e86109d58Matthew Williamspackage com.android.demo.jobSchedulerApp;
1813b0241248716b126a538457828a964e86109d58Matthew Williams
1913b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.app.Activity;
207060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.JobInfo;
217060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.JobParameters;
22f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williamsimport android.app.job.JobScheduler;
2313b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.content.ComponentName;
24f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williamsimport android.content.Context;
2513b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.content.Intent;
2613b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.content.res.Resources;
2713b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.Bundle;
2813b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.Handler;
2913b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.Message;
3013b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.os.Messenger;
3113b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.text.TextUtils;
3213b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.view.View;
33f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williamsimport android.widget.CheckBox;
3413b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.widget.EditText;
3513b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.widget.RadioButton;
3613b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.widget.TextView;
3713b0241248716b126a538457828a964e86109d58Matthew Williamsimport android.widget.Toast;
3813b0241248716b126a538457828a964e86109d58Matthew Williams
3913b0241248716b126a538457828a964e86109d58Matthew Williamsimport com.android.demo.jobSchedulerApp.service.TestJobService;
4013b0241248716b126a538457828a964e86109d58Matthew Williams
4113b0241248716b126a538457828a964e86109d58Matthew Williamspublic class MainActivity extends Activity {
4213b0241248716b126a538457828a964e86109d58Matthew Williams
4313b0241248716b126a538457828a964e86109d58Matthew Williams    private static final String TAG = "MainActivity";
4413b0241248716b126a538457828a964e86109d58Matthew Williams
4513b0241248716b126a538457828a964e86109d58Matthew Williams    public static final int MSG_UNCOLOUR_START = 0;
4613b0241248716b126a538457828a964e86109d58Matthew Williams    public static final int MSG_UNCOLOUR_STOP = 1;
4713b0241248716b126a538457828a964e86109d58Matthew Williams    public static final int MSG_SERVICE_OBJ = 2;
4813b0241248716b126a538457828a964e86109d58Matthew Williams
4913b0241248716b126a538457828a964e86109d58Matthew Williams    @Override
5013b0241248716b126a538457828a964e86109d58Matthew Williams    public void onCreate(Bundle savedInstanceState) {
5113b0241248716b126a538457828a964e86109d58Matthew Williams        super.onCreate(savedInstanceState);
5213b0241248716b126a538457828a964e86109d58Matthew Williams        setContentView(R.layout.activity_main);
5313b0241248716b126a538457828a964e86109d58Matthew Williams        Resources res = getResources();
544a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette        defaultColor = getColor(R.color.none_received);
554a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette        startJobColor = getColor(R.color.start_received);
564a357cd2e55293402d7172766f7f9419815fc1e8Alan Viverette        stopJobColor = getColor(R.color.stop_received);
5713b0241248716b126a538457828a964e86109d58Matthew Williams
5813b0241248716b126a538457828a964e86109d58Matthew Williams        // Set up UI.
5913b0241248716b126a538457828a964e86109d58Matthew Williams        mShowStartView = (TextView) findViewById(R.id.onstart_textview);
6013b0241248716b126a538457828a964e86109d58Matthew Williams        mShowStopView = (TextView) findViewById(R.id.onstop_textview);
6113b0241248716b126a538457828a964e86109d58Matthew Williams        mParamsTextView = (TextView) findViewById(R.id.task_params);
6213b0241248716b126a538457828a964e86109d58Matthew Williams        mDelayEditText = (EditText) findViewById(R.id.delay_time);
6313b0241248716b126a538457828a964e86109d58Matthew Williams        mDeadlineEditText = (EditText) findViewById(R.id.deadline_time);
6413b0241248716b126a538457828a964e86109d58Matthew Williams        mWiFiConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_unmetered);
6513b0241248716b126a538457828a964e86109d58Matthew Williams        mAnyConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_any);
66f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging);
67f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle);
689ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams        mIsPersistedCheckbox = (CheckBox) findViewById(R.id.checkbox_persisted);
699ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams
7013b0241248716b126a538457828a964e86109d58Matthew Williams        mServiceComponent = new ComponentName(this, TestJobService.class);
7113b0241248716b126a538457828a964e86109d58Matthew Williams        // Start service and provide it a way to communicate with us.
7213b0241248716b126a538457828a964e86109d58Matthew Williams        Intent startServiceIntent = new Intent(this, TestJobService.class);
7313b0241248716b126a538457828a964e86109d58Matthew Williams        startServiceIntent.putExtra("messenger", new Messenger(mHandler));
7413b0241248716b126a538457828a964e86109d58Matthew Williams        startService(startServiceIntent);
7513b0241248716b126a538457828a964e86109d58Matthew Williams    }
7613b0241248716b126a538457828a964e86109d58Matthew Williams    // UI fields.
7713b0241248716b126a538457828a964e86109d58Matthew Williams    int defaultColor;
7813b0241248716b126a538457828a964e86109d58Matthew Williams    int startJobColor;
7913b0241248716b126a538457828a964e86109d58Matthew Williams    int stopJobColor;
8013b0241248716b126a538457828a964e86109d58Matthew Williams
8113b0241248716b126a538457828a964e86109d58Matthew Williams    TextView mShowStartView;
8213b0241248716b126a538457828a964e86109d58Matthew Williams    TextView mShowStopView;
8313b0241248716b126a538457828a964e86109d58Matthew Williams    TextView mParamsTextView;
8413b0241248716b126a538457828a964e86109d58Matthew Williams    EditText mDelayEditText;
8513b0241248716b126a538457828a964e86109d58Matthew Williams    EditText mDeadlineEditText;
8613b0241248716b126a538457828a964e86109d58Matthew Williams    RadioButton mWiFiConnectivityRadioButton;
8713b0241248716b126a538457828a964e86109d58Matthew Williams    RadioButton mAnyConnectivityRadioButton;
88f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    CheckBox mRequiresChargingCheckBox;
89f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    CheckBox mRequiresIdleCheckbox;
909ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams    CheckBox mIsPersistedCheckbox;
91f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams
9213b0241248716b126a538457828a964e86109d58Matthew Williams    ComponentName mServiceComponent;
937060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    /** Service object to interact scheduled jobs. */
9413b0241248716b126a538457828a964e86109d58Matthew Williams    TestJobService mTestService;
9513b0241248716b126a538457828a964e86109d58Matthew Williams
967060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private static int kJobId = 0;
9713b0241248716b126a538457828a964e86109d58Matthew Williams
9813b0241248716b126a538457828a964e86109d58Matthew Williams    Handler mHandler = new Handler(/* default looper */) {
9913b0241248716b126a538457828a964e86109d58Matthew Williams        @Override
10013b0241248716b126a538457828a964e86109d58Matthew Williams        public void handleMessage(Message msg) {
10113b0241248716b126a538457828a964e86109d58Matthew Williams            switch (msg.what) {
10213b0241248716b126a538457828a964e86109d58Matthew Williams                case MSG_UNCOLOUR_START:
10313b0241248716b126a538457828a964e86109d58Matthew Williams                    mShowStartView.setBackgroundColor(defaultColor);
10413b0241248716b126a538457828a964e86109d58Matthew Williams                    break;
10513b0241248716b126a538457828a964e86109d58Matthew Williams                case MSG_UNCOLOUR_STOP:
10613b0241248716b126a538457828a964e86109d58Matthew Williams                    mShowStopView.setBackgroundColor(defaultColor);
10713b0241248716b126a538457828a964e86109d58Matthew Williams                    break;
10813b0241248716b126a538457828a964e86109d58Matthew Williams                case MSG_SERVICE_OBJ:
10913b0241248716b126a538457828a964e86109d58Matthew Williams                    mTestService = (TestJobService) msg.obj;
11013b0241248716b126a538457828a964e86109d58Matthew Williams                    mTestService.setUiCallback(MainActivity.this);
11113b0241248716b126a538457828a964e86109d58Matthew Williams            }
11213b0241248716b126a538457828a964e86109d58Matthew Williams        }
11313b0241248716b126a538457828a964e86109d58Matthew Williams    };
11413b0241248716b126a538457828a964e86109d58Matthew Williams
11513b0241248716b126a538457828a964e86109d58Matthew Williams    private boolean ensureTestService() {
11613b0241248716b126a538457828a964e86109d58Matthew Williams        if (mTestService == null) {
11713b0241248716b126a538457828a964e86109d58Matthew Williams            Toast.makeText(MainActivity.this, "Service null, never got callback?",
11813b0241248716b126a538457828a964e86109d58Matthew Williams                    Toast.LENGTH_SHORT).show();
11913b0241248716b126a538457828a964e86109d58Matthew Williams            return false;
12013b0241248716b126a538457828a964e86109d58Matthew Williams        }
12113b0241248716b126a538457828a964e86109d58Matthew Williams        return true;
12213b0241248716b126a538457828a964e86109d58Matthew Williams    }
12313b0241248716b126a538457828a964e86109d58Matthew Williams
12413b0241248716b126a538457828a964e86109d58Matthew Williams    /**
1257060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * UI onclick listener to schedule a job. What this job is is defined in
12613b0241248716b126a538457828a964e86109d58Matthew Williams     * TestJobService#scheduleJob()
12713b0241248716b126a538457828a964e86109d58Matthew Williams     */
12813b0241248716b126a538457828a964e86109d58Matthew Williams    public void scheduleJob(View v) {
12913b0241248716b126a538457828a964e86109d58Matthew Williams        if (!ensureTestService()) {
13013b0241248716b126a538457828a964e86109d58Matthew Williams            return;
13113b0241248716b126a538457828a964e86109d58Matthew Williams        }
13213b0241248716b126a538457828a964e86109d58Matthew Williams
1337060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
13413b0241248716b126a538457828a964e86109d58Matthew Williams
13513b0241248716b126a538457828a964e86109d58Matthew Williams        String delay = mDelayEditText.getText().toString();
13613b0241248716b126a538457828a964e86109d58Matthew Williams        if (delay != null && !TextUtils.isEmpty(delay)) {
137f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams            builder.setMinimumLatency(Long.valueOf(delay) * 1000);
13813b0241248716b126a538457828a964e86109d58Matthew Williams        }
13913b0241248716b126a538457828a964e86109d58Matthew Williams        String deadline = mDeadlineEditText.getText().toString();
14013b0241248716b126a538457828a964e86109d58Matthew Williams        if (deadline != null && !TextUtils.isEmpty(deadline)) {
141f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams            builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
14213b0241248716b126a538457828a964e86109d58Matthew Williams        }
143f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
144f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
14513b0241248716b126a538457828a964e86109d58Matthew Williams        if (requiresUnmetered) {
146d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
14713b0241248716b126a538457828a964e86109d58Matthew Williams        } else if (requiresAnyConnectivity) {
148d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
14913b0241248716b126a538457828a964e86109d58Matthew Williams        }
150f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
151f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
152d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        builder.setPersisted(mIsPersistedCheckbox.isChecked());
15313b0241248716b126a538457828a964e86109d58Matthew Williams        mTestService.scheduleJob(builder.build());
15413b0241248716b126a538457828a964e86109d58Matthew Williams
15513b0241248716b126a538457828a964e86109d58Matthew Williams    }
15613b0241248716b126a538457828a964e86109d58Matthew Williams
157f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    public void cancelAllJobs(View v) {
158f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        JobScheduler tm =
159f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
160f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        tm.cancelAll();
161f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    }
162f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams
16313b0241248716b126a538457828a964e86109d58Matthew Williams    /**
1647060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * UI onclick listener to call jobFinished() in our service.
16513b0241248716b126a538457828a964e86109d58Matthew Williams     */
16613b0241248716b126a538457828a964e86109d58Matthew Williams    public void finishJob(View v) {
16713b0241248716b126a538457828a964e86109d58Matthew Williams        if (!ensureTestService()) {
16813b0241248716b126a538457828a964e86109d58Matthew Williams            return;
16913b0241248716b126a538457828a964e86109d58Matthew Williams        }
1707060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        mTestService.callJobFinished();
17113b0241248716b126a538457828a964e86109d58Matthew Williams        mParamsTextView.setText("");
17213b0241248716b126a538457828a964e86109d58Matthew Williams    }
17313b0241248716b126a538457828a964e86109d58Matthew Williams
1747060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public void onReceivedStartJob(JobParameters params) {
17513b0241248716b126a538457828a964e86109d58Matthew Williams        mShowStartView.setBackgroundColor(startJobColor);
17613b0241248716b126a538457828a964e86109d58Matthew Williams        Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START);
17713b0241248716b126a538457828a964e86109d58Matthew Williams        mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second.
1787060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras());
17913b0241248716b126a538457828a964e86109d58Matthew Williams    }
18013b0241248716b126a538457828a964e86109d58Matthew Williams
1817060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public void onReceivedStopJob() {
18213b0241248716b126a538457828a964e86109d58Matthew Williams        mShowStopView.setBackgroundColor(stopJobColor);
18313b0241248716b126a538457828a964e86109d58Matthew Williams        Message m = Message.obtain(mHandler, MSG_UNCOLOUR_STOP);
18413b0241248716b126a538457828a964e86109d58Matthew Williams        mHandler.sendMessageDelayed(m, 2000L); // uncolour in 1 second.
18513b0241248716b126a538457828a964e86109d58Matthew Williams        mParamsTextView.setText("");
18613b0241248716b126a538457828a964e86109d58Matthew Williams    }
18713b0241248716b126a538457828a964e86109d58Matthew Williams}
188