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.
5951efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mShowStartView = findViewById(R.id.onstart_textview);
6051efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mShowStopView = findViewById(R.id.onstop_textview);
6151efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mParamsTextView = findViewById(R.id.task_params);
6251efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mDelayEditText = findViewById(R.id.delay_time);
6351efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mDeadlineEditText = findViewById(R.id.deadline_time);
6451efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mWiFiConnectivityRadioButton = findViewById(R.id.checkbox_unmetered);
6551efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mAnyConnectivityRadioButton = findViewById(R.id.checkbox_any);
6606f0852ead5524c09987fc9da5129390a2dba251Christopher Tate        mCellConnectivityRadioButton = findViewById(R.id.checkbox_metered);
6751efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mRequiresChargingCheckBox = findViewById(R.id.checkbox_charging);
6851efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mRequiresIdleCheckbox = findViewById(R.id.checkbox_idle);
6951efddbd3bb304de2dd47fa8cd1114ac555958bbAlan Viverette        mIsPersistedCheckbox = findViewById(R.id.checkbox_persisted);
709ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams
7113b0241248716b126a538457828a964e86109d58Matthew Williams        mServiceComponent = new ComponentName(this, TestJobService.class);
7213b0241248716b126a538457828a964e86109d58Matthew Williams        // Start service and provide it a way to communicate with us.
7313b0241248716b126a538457828a964e86109d58Matthew Williams        Intent startServiceIntent = new Intent(this, TestJobService.class);
7413b0241248716b126a538457828a964e86109d58Matthew Williams        startServiceIntent.putExtra("messenger", new Messenger(mHandler));
7513b0241248716b126a538457828a964e86109d58Matthew Williams        startService(startServiceIntent);
7613b0241248716b126a538457828a964e86109d58Matthew Williams    }
7713b0241248716b126a538457828a964e86109d58Matthew Williams    // UI fields.
7813b0241248716b126a538457828a964e86109d58Matthew Williams    int defaultColor;
7913b0241248716b126a538457828a964e86109d58Matthew Williams    int startJobColor;
8013b0241248716b126a538457828a964e86109d58Matthew Williams    int stopJobColor;
8113b0241248716b126a538457828a964e86109d58Matthew Williams
8213b0241248716b126a538457828a964e86109d58Matthew Williams    TextView mShowStartView;
8313b0241248716b126a538457828a964e86109d58Matthew Williams    TextView mShowStopView;
8413b0241248716b126a538457828a964e86109d58Matthew Williams    TextView mParamsTextView;
8513b0241248716b126a538457828a964e86109d58Matthew Williams    EditText mDelayEditText;
8613b0241248716b126a538457828a964e86109d58Matthew Williams    EditText mDeadlineEditText;
8713b0241248716b126a538457828a964e86109d58Matthew Williams    RadioButton mWiFiConnectivityRadioButton;
8813b0241248716b126a538457828a964e86109d58Matthew Williams    RadioButton mAnyConnectivityRadioButton;
8906f0852ead5524c09987fc9da5129390a2dba251Christopher Tate    RadioButton mCellConnectivityRadioButton;
90f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    CheckBox mRequiresChargingCheckBox;
91f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    CheckBox mRequiresIdleCheckbox;
929ae3dbeefcd6bc139c74bfe3d51de823e3be4b4bMatthew Williams    CheckBox mIsPersistedCheckbox;
93f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams
9413b0241248716b126a538457828a964e86109d58Matthew Williams    ComponentName mServiceComponent;
957060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    /** Service object to interact scheduled jobs. */
9613b0241248716b126a538457828a964e86109d58Matthew Williams    TestJobService mTestService;
9713b0241248716b126a538457828a964e86109d58Matthew Williams
987060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private static int kJobId = 0;
9913b0241248716b126a538457828a964e86109d58Matthew Williams
10013b0241248716b126a538457828a964e86109d58Matthew Williams    Handler mHandler = new Handler(/* default looper */) {
10113b0241248716b126a538457828a964e86109d58Matthew Williams        @Override
10213b0241248716b126a538457828a964e86109d58Matthew Williams        public void handleMessage(Message msg) {
10313b0241248716b126a538457828a964e86109d58Matthew Williams            switch (msg.what) {
10413b0241248716b126a538457828a964e86109d58Matthew Williams                case MSG_UNCOLOUR_START:
10513b0241248716b126a538457828a964e86109d58Matthew Williams                    mShowStartView.setBackgroundColor(defaultColor);
10613b0241248716b126a538457828a964e86109d58Matthew Williams                    break;
10713b0241248716b126a538457828a964e86109d58Matthew Williams                case MSG_UNCOLOUR_STOP:
10813b0241248716b126a538457828a964e86109d58Matthew Williams                    mShowStopView.setBackgroundColor(defaultColor);
10913b0241248716b126a538457828a964e86109d58Matthew Williams                    break;
11013b0241248716b126a538457828a964e86109d58Matthew Williams                case MSG_SERVICE_OBJ:
11113b0241248716b126a538457828a964e86109d58Matthew Williams                    mTestService = (TestJobService) msg.obj;
11213b0241248716b126a538457828a964e86109d58Matthew Williams                    mTestService.setUiCallback(MainActivity.this);
11313b0241248716b126a538457828a964e86109d58Matthew Williams            }
11413b0241248716b126a538457828a964e86109d58Matthew Williams        }
11513b0241248716b126a538457828a964e86109d58Matthew Williams    };
11613b0241248716b126a538457828a964e86109d58Matthew Williams
11713b0241248716b126a538457828a964e86109d58Matthew Williams    private boolean ensureTestService() {
11813b0241248716b126a538457828a964e86109d58Matthew Williams        if (mTestService == null) {
11913b0241248716b126a538457828a964e86109d58Matthew Williams            Toast.makeText(MainActivity.this, "Service null, never got callback?",
12013b0241248716b126a538457828a964e86109d58Matthew Williams                    Toast.LENGTH_SHORT).show();
12113b0241248716b126a538457828a964e86109d58Matthew Williams            return false;
12213b0241248716b126a538457828a964e86109d58Matthew Williams        }
12313b0241248716b126a538457828a964e86109d58Matthew Williams        return true;
12413b0241248716b126a538457828a964e86109d58Matthew Williams    }
12513b0241248716b126a538457828a964e86109d58Matthew Williams
12613b0241248716b126a538457828a964e86109d58Matthew Williams    /**
1277060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * UI onclick listener to schedule a job. What this job is is defined in
12813b0241248716b126a538457828a964e86109d58Matthew Williams     * TestJobService#scheduleJob()
12913b0241248716b126a538457828a964e86109d58Matthew Williams     */
13013b0241248716b126a538457828a964e86109d58Matthew Williams    public void scheduleJob(View v) {
13113b0241248716b126a538457828a964e86109d58Matthew Williams        if (!ensureTestService()) {
13213b0241248716b126a538457828a964e86109d58Matthew Williams            return;
13313b0241248716b126a538457828a964e86109d58Matthew Williams        }
13413b0241248716b126a538457828a964e86109d58Matthew Williams
1357060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
13613b0241248716b126a538457828a964e86109d58Matthew Williams
13713b0241248716b126a538457828a964e86109d58Matthew Williams        String delay = mDelayEditText.getText().toString();
13813b0241248716b126a538457828a964e86109d58Matthew Williams        if (delay != null && !TextUtils.isEmpty(delay)) {
13928532d00267c73b8e0fa5235169fa72c414e1cd0Tobias Thierer            builder.setMinimumLatency(Long.parseLong(delay) * 1000);
14013b0241248716b126a538457828a964e86109d58Matthew Williams        }
14113b0241248716b126a538457828a964e86109d58Matthew Williams        String deadline = mDeadlineEditText.getText().toString();
14213b0241248716b126a538457828a964e86109d58Matthew Williams        if (deadline != null && !TextUtils.isEmpty(deadline)) {
14328532d00267c73b8e0fa5235169fa72c414e1cd0Tobias Thierer            builder.setOverrideDeadline(Long.parseLong(deadline) * 1000);
14413b0241248716b126a538457828a964e86109d58Matthew Williams        }
145f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
14606f0852ead5524c09987fc9da5129390a2dba251Christopher Tate        boolean requiresMetered = mCellConnectivityRadioButton.isChecked();
147f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
14813b0241248716b126a538457828a964e86109d58Matthew Williams        if (requiresUnmetered) {
149d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
15006f0852ead5524c09987fc9da5129390a2dba251Christopher Tate        } else if (requiresMetered) {
15106f0852ead5524c09987fc9da5129390a2dba251Christopher Tate            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_METERED);
15213b0241248716b126a538457828a964e86109d58Matthew Williams        } else if (requiresAnyConnectivity) {
153d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
15413b0241248716b126a538457828a964e86109d58Matthew Williams        }
155f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
156f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
157d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams        builder.setPersisted(mIsPersistedCheckbox.isChecked());
15813b0241248716b126a538457828a964e86109d58Matthew Williams        mTestService.scheduleJob(builder.build());
15913b0241248716b126a538457828a964e86109d58Matthew Williams
16013b0241248716b126a538457828a964e86109d58Matthew Williams    }
16113b0241248716b126a538457828a964e86109d58Matthew Williams
162f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    public void cancelAllJobs(View v) {
163f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        JobScheduler tm =
164f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
165f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams        tm.cancelAll();
166f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams    }
167f7c4fddf44dd94162d26635bbd703ffeb392c675Matthew Williams
16813b0241248716b126a538457828a964e86109d58Matthew Williams    /**
1697060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * UI onclick listener to call jobFinished() in our service.
17013b0241248716b126a538457828a964e86109d58Matthew Williams     */
17113b0241248716b126a538457828a964e86109d58Matthew Williams    public void finishJob(View v) {
17213b0241248716b126a538457828a964e86109d58Matthew Williams        if (!ensureTestService()) {
17313b0241248716b126a538457828a964e86109d58Matthew Williams            return;
17413b0241248716b126a538457828a964e86109d58Matthew Williams        }
1757060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        mTestService.callJobFinished();
17613b0241248716b126a538457828a964e86109d58Matthew Williams        mParamsTextView.setText("");
17713b0241248716b126a538457828a964e86109d58Matthew Williams    }
17813b0241248716b126a538457828a964e86109d58Matthew Williams
1797060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public void onReceivedStartJob(JobParameters params) {
18013b0241248716b126a538457828a964e86109d58Matthew Williams        mShowStartView.setBackgroundColor(startJobColor);
18113b0241248716b126a538457828a964e86109d58Matthew Williams        Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START);
18213b0241248716b126a538457828a964e86109d58Matthew Williams        mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second.
1837060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras());
18413b0241248716b126a538457828a964e86109d58Matthew Williams    }
18513b0241248716b126a538457828a964e86109d58Matthew Williams
1867060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public void onReceivedStopJob() {
18713b0241248716b126a538457828a964e86109d58Matthew Williams        mShowStopView.setBackgroundColor(stopJobColor);
18813b0241248716b126a538457828a964e86109d58Matthew Williams        Message m = Message.obtain(mHandler, MSG_UNCOLOUR_STOP);
18913b0241248716b126a538457828a964e86109d58Matthew Williams        mHandler.sendMessageDelayed(m, 2000L); // uncolour in 1 second.
19013b0241248716b126a538457828a964e86109d58Matthew Williams        mParamsTextView.setText("");
19113b0241248716b126a538457828a964e86109d58Matthew Williams    }
19213b0241248716b126a538457828a964e86109d58Matthew Williams}
193