Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f7c4fddf authored by Matthew Williams's avatar Matthew Williams
Browse files

Flesh out job scheduler test app

adds the rest of the constraints - charging, idle, and allows you
to cancel all.
Change-Id: I43b7cac94446f6860ca0387440b3c8f995a2c0f3
parent d7e16851
Loading
Loading
Loading
Loading
+72 −33
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

@@ -54,6 +54,7 @@
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/constraints"
            android:layout_margin="15dp"
            android:textSize="18dp"/>
        <LinearLayout
            android:layout_width="match_parent"
@@ -112,14 +113,52 @@
                        android:layout_height="wrap_content"
                        android:inputType="number"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/charging_caption"
                        android:layout_marginRight="15dp"/>
                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkbox_charging"
                        android:text="@string/charging_text"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/idle_caption"
                        android:layout_marginRight="15dp"/>
                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkbox_idle"
                        android:text="@string/idle_mode_text"/>
                </LinearLayout>

            </LinearLayout>
        <Button
            android:id="@+id/schedule_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:onClick="scheduleJob"
            android:text="@string/schedule_job_button_text"/>
        <Button
            android:id="@+id/cancel_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:onClick="cancelAllJobs"
            android:text="@string/cancel_all_jobs_button_text"/>
    </LinearLayout>
</LinearLayout>
</ScrollView>
+5 −1
Original line number Diff line number Diff line
@@ -20,9 +20,13 @@ limitations under the License.
    <string name="onstarttask">onStartTask</string>
    <string name="defaultparamtext">task params will show up here.</string>
    <string name="schedule_job_button_text">Schedule Job</string>
    <string name="cancel_all_jobs_button_text">Cancel all</string>
    <string name="app_name">Job Scheduler Test</string>
    <string name="finish_job_button_text">taskFinished</string>
    <string name="manual_sync_text">Manual Sync</string>
    <string name="idle_mode_text">Requires device in idle mode.</string>
    <string name="charging_caption">Charging:</string>
    <string name="charging_text">Requires device plugged in.</string>
    <string name="idle_caption">Idle:</string>
    <string name="constraints">Constraints</string>
    <string name="connectivity">Connectivity:</string>
    <string name="any">Any</string>
+20 −5
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ package com.android.demo.jobSchedulerApp;
import android.app.Activity;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
@@ -28,6 +30,7 @@ import android.os.Message;
import android.os.Messenger;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
@@ -60,7 +63,8 @@ public class MainActivity extends Activity {
        mDeadlineEditText = (EditText) findViewById(R.id.deadline_time);
        mWiFiConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_unmetered);
        mAnyConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_any);

        mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging);
        mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle);
        mServiceComponent = new ComponentName(this, TestJobService.class);
        // Start service and provide it a way to communicate with us.
        Intent startServiceIntent = new Intent(this, TestJobService.class);
@@ -79,6 +83,9 @@ public class MainActivity extends Activity {
    EditText mDeadlineEditText;
    RadioButton mWiFiConnectivityRadioButton;
    RadioButton mAnyConnectivityRadioButton;
    CheckBox mRequiresChargingCheckBox;
    CheckBox mRequiresIdleCheckbox;

    ComponentName mServiceComponent;
    /** Service object to interact scheduled jobs. */
    TestJobService mTestService;
@@ -124,24 +131,32 @@ public class MainActivity extends Activity {

        String delay = mDelayEditText.getText().toString();
        if (delay != null && !TextUtils.isEmpty(delay)) {
            builder.setMinimumLatency(Long.valueOf(delay));
            builder.setMinimumLatency(Long.valueOf(delay) * 1000);
        }
        String deadline = mDeadlineEditText.getText().toString();
        if (deadline != null && !TextUtils.isEmpty(deadline)) {
            builder.setOverrideDeadline(Long.valueOf(deadline));
            builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
        }
        boolean requiresUnmetered = mWiFiConnectivityRadioButton.isSelected();
        boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isSelected();
        boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
        boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
        if (requiresUnmetered) {
            builder.setRequiredNetworkCapabilities(JobInfo.NetworkType.UNMETERED);
        } else if (requiresAnyConnectivity) {
            builder.setRequiredNetworkCapabilities(JobInfo.NetworkType.ANY);
        }
        builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
        builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());

        mTestService.scheduleJob(builder.build());

    }

    public void cancelAllJobs(View v) {
        JobScheduler tm =
                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.cancelAll();
    }

    /**
     * UI onclick listener to call jobFinished() in our service.
     */