blob: 1d36b48b2fc942cf14a3d9892e1943d98d0bbbd4 [file] [log] [blame]
Ben Lin8e912582016-09-23 14:25:14 -07001/*
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
17package com.android.documentsui;
18
19import android.annotation.CallSuper;
20import android.os.AsyncTask;
21import android.os.Handler;
22
23import com.android.documentsui.base.CheckedTask;
24import com.android.documentsui.base.DocumentInfo;
25
26/**
27 * A {@link CheckedTask} that takes and query SAF to obtain the
28 * {@link DocumentInfo} of its root document and call supplied callback to handle the
29 * {@link DocumentInfo}.
30 */
31public abstract class TimeoutTask<Input, Output> extends CheckedTask<Input, Output> {
32 private static final int DEFAULT_TIMEOUT = -1;
33
34 private long mTimeout = DEFAULT_TIMEOUT;
35
36 public TimeoutTask(Check check) {
37 super(check);
38 }
39
40 public void setTimeout(long timeout) {
41 mTimeout = timeout;
42 }
43
44 @CallSuper
45 @Override
46 protected void prepare() {
47 if (mTimeout < 0) {
48 return;
49 }
50
51 Handler handler = new Handler();
52 handler.postDelayed(() -> {
53 if (getStatus() == AsyncTask.Status.RUNNING) {
54 cancel(true);
55 this.finish(null);
56 }
57 }, mTimeout);
58 }
59
60}