1 /* 2 * Copyright (C) 2015 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 package com.google.android.car.kitchensink.job; 17 18 import android.app.job.JobInfo; 19 import android.app.job.JobScheduler; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.SharedPreferences; 23 import android.os.Bundle; 24 import android.os.PersistableBundle; 25 import android.support.annotation.Nullable; 26 import android.support.v4.app.Fragment; 27 import android.util.Log; 28 import android.view.LayoutInflater; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.widget.Button; 32 import android.widget.CheckBox; 33 import android.widget.EditText; 34 import android.widget.RadioGroup; 35 import android.widget.TextView; 36 import android.widget.Toast; 37 38 import com.google.android.car.kitchensink.R; 39 40 import java.util.List; 41 42 public class JobSchedulerFragment extends Fragment { 43 private static final String TAG = "JobSchedulerFragment"; 44 private static final String PREFS_NEXT_JOB_ID = "next_job_id"; 45 46 private Button mScheduleButton; 47 private Button mRefreshButton; 48 private Button mCancelButton; 49 private CheckBox mRequireCharging; 50 private CheckBox mRequireIdle; 51 private CheckBox mRequirePersisted; 52 private RadioGroup mNetworkGroup; 53 private EditText mDishNum; 54 private TextView mJobInfo; 55 private JobScheduler mJobScheduler; 56 57 @Nullable 58 @Override 59 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 60 @Nullable Bundle savedInstanceState) { 61 View v = inflater.inflate(R.layout.job_scheduler, container, false); 62 mScheduleButton = (Button) v.findViewById(R.id.schedule_button); 63 mScheduleButton.setOnClickListener(new View.OnClickListener() { 64 @Override 65 public void onClick(View view) { 66 scheduleJob(); 67 } 68 }); 69 mRefreshButton = (Button) v.findViewById(R.id.refresh_button); 70 mRefreshButton.setOnClickListener(new View.OnClickListener() { 71 @Override 72 public void onClick(View view) { 73 refreshCurrentJobs(); 74 } 75 }); 76 77 mNetworkGroup = (RadioGroup) v.findViewById(R.id.network_group); 78 mDishNum = (EditText) v.findViewById(R.id.dish_num); 79 mRequireCharging = (CheckBox) v.findViewById(R.id.require_charging); 80 mRequireIdle = (CheckBox) v.findViewById(R.id.require_idle); 81 mRequirePersisted = (CheckBox) v.findViewById(R.id.require_persisted); 82 mJobScheduler = (JobScheduler) getContext() 83 .getSystemService(Context.JOB_SCHEDULER_SERVICE); 84 85 mJobInfo = (TextView) v.findViewById(R.id.current_jobs); 86 87 mCancelButton = (Button) v.findViewById(R.id.cancel_button); 88 mCancelButton.setOnClickListener(new View.OnClickListener() { 89 @Override 90 public void onClick(View view) { 91 mJobScheduler.cancelAll(); 92 refreshCurrentJobs(); 93 } 94 }); 95 return v; 96 } 97 98 @Override 99 public void onResume() { 100 super.onResume(); 101 refreshCurrentJobs(); 102 } 103 104 private void refreshCurrentJobs() { 105 StringBuilder sb = new StringBuilder(); 106 List<JobInfo> jobs = mJobScheduler.getAllPendingJobs(); 107 for (JobInfo job : jobs) { 108 sb.append("JobId: "); 109 sb.append(job.getId()); 110 sb.append("\nDishCount: "); 111 sb.append(job.getExtras().getInt(DishService.EXTRA_DISH_COUNT, 0)); 112 sb.append("\n"); 113 } 114 mJobInfo.setText(sb.toString()); 115 } 116 117 private void scheduleJob() { 118 ComponentName jobComponentName = new ComponentName(getContext(), DishService.class); 119 SharedPreferences prefs = getContext() 120 .getSharedPreferences(PREFS_NEXT_JOB_ID, Context.MODE_PRIVATE); 121 int jobId = prefs.getInt(PREFS_NEXT_JOB_ID, 0); 122 PersistableBundle bundle = new PersistableBundle(); 123 int count = 50; 124 try { 125 count = Integer.valueOf(mDishNum.getText().toString()); 126 } catch (NumberFormatException e) { 127 Log.e(TAG, "NOT A NUMBER!!!"); 128 } 129 130 int selected = mNetworkGroup.getCheckedRadioButtonId(); 131 int networkType = JobInfo.NETWORK_TYPE_ANY; 132 switch (selected) { 133 case R.id.network_none: 134 networkType = JobInfo.NETWORK_TYPE_NONE; 135 break; 136 case R.id.network_unmetered: 137 networkType = JobInfo.NETWORK_TYPE_UNMETERED; 138 break; 139 case R.id.network_any: 140 networkType = JobInfo.NETWORK_TYPE_ANY; 141 break; 142 } 143 bundle.putInt(DishService.EXTRA_DISH_COUNT, count); 144 JobInfo jobInfo = new JobInfo.Builder(jobId, jobComponentName) 145 .setRequiresCharging(mRequireCharging.isChecked()) 146 .setRequiresDeviceIdle(mRequireIdle.isChecked()) 147 // TODO: figure out why we crash here even we hold 148 // the RECEIVE_BOOT_COMPLETE permission 149 //.setPersisted(mRequirePersisted.isChecked()) 150 .setExtras(bundle) 151 .setRequiredNetworkType(networkType) 152 .build(); 153 154 155 mJobScheduler.schedule(jobInfo); 156 Toast.makeText(getContext(), "Scheduled: " + jobInfo, Toast.LENGTH_LONG ).show(); 157 158 Log.d(TAG, "Scheduled a job: " + jobInfo); 159 SharedPreferences.Editor editor = prefs.edit(); 160 editor.putInt(PREFS_NEXT_JOB_ID, jobId + 1); 161 editor.commit(); 162 163 refreshCurrentJobs(); 164 } 165 } 166