Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.documentsui; |
| 18 | |
| 19 | import android.annotation.CallSuper; |
| 20 | import android.os.AsyncTask; |
| 21 | import android.os.Handler; |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 22 | import android.os.Looper; |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 23 | |
| 24 | import com.android.documentsui.base.CheckedTask; |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 27 | * A {@link CheckedTask} that will timeout after a certain period of time, and do any properly clean |
| 28 | * up necessary before ending itself. |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 29 | */ |
| 30 | public abstract class TimeoutTask<Input, Output> extends CheckedTask<Input, Output> { |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 31 | public static final int DEFAULT_TIMEOUT = -1; |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 32 | |
| 33 | private long mTimeout = DEFAULT_TIMEOUT; |
| 34 | |
Ben Lin | 30b0dc1 | 2017-03-07 15:37:16 -0800 | [diff] [blame] | 35 | public TimeoutTask(Check check, long timeout) { |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 36 | super(check); |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 37 | mTimeout = timeout; |
| 38 | } |
| 39 | |
| 40 | @CallSuper |
| 41 | @Override |
| 42 | protected void prepare() { |
| 43 | if (mTimeout < 0) { |
| 44 | return; |
| 45 | } |
| 46 | |
Ben Lin | e9abd2d | 2016-12-06 11:39:52 -0800 | [diff] [blame] | 47 | // Need to initialize handler to main Looper so it can initialize correctly in test cases |
| 48 | // Instrumentation threads don't have looper initialized |
| 49 | Handler handler = new Handler(Looper.getMainLooper()); |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 50 | handler.postDelayed(() -> { |
| 51 | if (getStatus() == AsyncTask.Status.RUNNING) { |
Ben Lin | bbb7d03 | 2016-11-15 13:35:41 -0800 | [diff] [blame] | 52 | onTimeout(); |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 53 | cancel(true); |
| 54 | this.finish(null); |
| 55 | } |
| 56 | }, mTimeout); |
| 57 | } |
| 58 | |
Ben Lin | 6537acd | 2016-11-16 12:06:21 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Override this do more proper clean up in case of timeout, such as using |
| 61 | * CancellationSignal#cancel. |
| 62 | */ |
| 63 | protected void onTimeout() {} |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 64 | } |