1 /* 2 * Copyright (C) 2017 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.server.cts.storaged; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.Handler; 22 import android.os.HandlerThread; 23 import android.os.IBinder; 24 import android.os.Looper; 25 import android.os.Message; 26 import android.os.Process; 27 28 import java.io.File; 29 import java.io.FileWriter; 30 import java.io.IOException; 31 32 public class SimpleIOService extends Service { 33 private Looper mServiceLooper; 34 private ServiceHandler mServiceHandler; 35 36 private final class ServiceHandler extends Handler { 37 public ServiceHandler(Looper looper) { 38 super(looper); 39 } 40 41 @Override 42 public void handleMessage(Message msg) { 43 File testFile = new File(getFilesDir(), "StoragedTest_Temp_BG"); 44 try { 45 char data[] = new char[4096]; 46 FileWriter w = new FileWriter(testFile); 47 w.write(data); 48 w.flush(); 49 w.close(); 50 } catch (IOException e) { 51 throw new RuntimeException(e); 52 } finally { 53 // Stop the service using the startId, so that we don't stop 54 // the service in the middle of handling another job 55 stopSelf(msg.arg1); 56 } 57 } 58 } 59 60 @Override 61 public void onCreate() { 62 // Start up the thread running the service. Note that we create a 63 // separate thread because the service normally runs in the process's 64 // main thread, which we don't want to block. We also make it 65 // background priority so CPU-intensive work will not disrupt our UI. 66 HandlerThread thread = new HandlerThread("ServiceStartArguments", 67 Process.THREAD_PRIORITY_BACKGROUND); 68 thread.start(); 69 70 // Get the HandlerThread's Looper and use it for our Handler 71 mServiceLooper = thread.getLooper(); 72 mServiceHandler = new ServiceHandler(mServiceLooper); 73 } 74 75 @Override 76 public int onStartCommand(Intent intent, int flags, int startId) { 77 Message msg = mServiceHandler.obtainMessage(); 78 msg.arg1 = startId; 79 mServiceHandler.sendMessage(msg); 80 81 return START_NOT_STICKY; 82 } 83 84 @Override 85 public IBinder onBind(Intent intent) { 86 return null; 87 } 88 } 89