1 /* 2 * Copyright (C) 2019 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 17 package com.android.nn.dogfood; 18 19 import android.app.job.JobInfo; 20 import android.app.job.JobScheduler; 21 import android.content.ComponentName; 22 import android.os.Bundle; 23 import android.util.Log; 24 import android.view.View; 25 import android.widget.Button; 26 import android.widget.TextView; 27 28 import androidx.appcompat.app.AppCompatActivity; 29 30 import com.android.nn.benchmark.core.TestModelsListLoader; 31 import com.android.nn.benchmark.util.TestExternalStorageActivity; 32 33 import java.io.IOException; 34 35 public class MainActivity extends AppCompatActivity { 36 37 private static final String TAG = "NN_BENCHMARK"; 38 private static final int JOB_FREQUENCY_MILLIS = 15 * 60 * 1000; // 15 minutes 39 private Button mStartStopButton; 40 private TextView mMessage; 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 TestExternalStorageActivity.testWriteExternalStorage(this, true); 47 48 mStartStopButton = (Button) findViewById(R.id.start_stop_button); 49 mMessage = (TextView) findViewById(R.id.message); 50 try { 51 TestModelsListLoader.parseFromAssets(getAssets()); 52 } catch (IOException e) { 53 Log.e(TAG, "Could not load models", e); 54 } 55 } 56 startStopTestClicked(View v)57 public void startStopTestClicked(View v) { 58 59 JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); 60 if (jobScheduler.getPendingJob(BenchmarkJobService.JOB_ID) == null) { 61 // no job is currently scheduled 62 ComponentName componentName = new ComponentName(this, BenchmarkJobService.class); 63 JobInfo jobInfo = 64 new JobInfo.Builder(BenchmarkJobService.JOB_ID, componentName) 65 .setPeriodic(JOB_FREQUENCY_MILLIS) 66 .build(); 67 jobScheduler.schedule(jobInfo); 68 mMessage.setText("Benchmark job scheduled, you can leave this app"); 69 mStartStopButton.setText(R.string.stop_button_text); 70 } else { 71 jobScheduler.cancel(BenchmarkJobService.JOB_ID); 72 mMessage.setText("Benchmark job cancelled"); 73 mStartStopButton.setText(R.string.start_button_text); 74 } 75 } 76 } 77