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.documentsui.picker;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.os.AsyncTask;
22 import android.os.SystemClock;
23 import com.android.documentsui.Metrics;
24 
25 // load & update mime type & repeatedly pick count in background
26 public class UpdatePickResultTask extends AsyncTask<Void, Void, Void> {
27     private Context mContext;
28     private PickResult mPickResult;
29     private PickCountRecordStorage mPickCountRecord;
30 
UpdatePickResultTask(Context context, PickResult pickResult)31     public UpdatePickResultTask(Context context, PickResult pickResult) {
32         this(context, pickResult, PickCountRecordStorage.create());
33     }
34 
UpdatePickResultTask(Context context, PickResult pickResult, PickCountRecordStorage pickCountRecord)35     public UpdatePickResultTask(Context context, PickResult pickResult,
36         PickCountRecordStorage pickCountRecord) {
37         mContext = context.getApplicationContext();
38         mPickResult = pickResult;
39         mPickCountRecord = pickCountRecord;
40     }
41 
42     @Override
onPreExecute()43     protected void onPreExecute() {
44         mPickResult.increaseDuration(SystemClock.uptimeMillis());
45     }
46 
47     @Override
doInBackground(Void... voids)48     protected Void doInBackground(Void... voids) {
49         Uri fileUri = mPickResult.getFileUri();
50         if (fileUri != null) {
51             mPickResult.setMimeType(
52                 Metrics.sanitizeMime(mContext.getContentResolver().getType(fileUri)));
53 
54             mPickResult.setRepeatedPickTimes(
55                 mPickCountRecord.increasePickCountRecord(mContext, fileUri));
56         }
57         return null;
58     }
59 
60     @Override
onPostExecute(Void aVoid)61     protected void onPostExecute(Void aVoid) {
62         Metrics.logPickResult(mPickResult);
63     }
64 
65 }
66