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 24 import com.android.documentsui.Metrics; 25 26 // load & update mime type & repeatedly pick count in background 27 public class UpdatePickResultTask extends AsyncTask<Void, Void, Void> { 28 private Context mContext; 29 private PickResult mPickResult; 30 private PickCountRecordStorage mPickCountRecord; 31 UpdatePickResultTask(Context context, PickResult pickResult)32 public UpdatePickResultTask(Context context, PickResult pickResult) { 33 this(context, pickResult, PickCountRecordStorage.create()); 34 } 35 UpdatePickResultTask(Context context, PickResult pickResult, PickCountRecordStorage pickCountRecord)36 public UpdatePickResultTask(Context context, PickResult pickResult, 37 PickCountRecordStorage pickCountRecord) { 38 mContext = context.getApplicationContext(); 39 mPickResult = pickResult; 40 mPickCountRecord = pickCountRecord; 41 } 42 43 @Override onPreExecute()44 protected void onPreExecute() { 45 mPickResult.increaseDuration(SystemClock.uptimeMillis()); 46 } 47 48 @Override doInBackground(Void... voids)49 protected Void doInBackground(Void... voids) { 50 Uri fileUri = mPickResult.getFileUri(); 51 if (fileUri != null) { 52 mPickResult.setMimeType( 53 Metrics.sanitizeMime(mContext.getContentResolver().getType(fileUri))); 54 55 mPickResult.setRepeatedPickTimes( 56 mPickCountRecord.increasePickCountRecord(mContext, fileUri)); 57 } 58 return null; 59 } 60 61 @Override onPostExecute(Void aVoid)62 protected void onPostExecute(Void aVoid) { 63 Metrics.logPickResult(mPickResult); 64 } 65 66 /** 67 * Check the status and only execute if task is pending. 68 */ safeExecute()69 public void safeExecute() { 70 if (getStatus() == Status.PENDING) { 71 execute(); 72 } 73 } 74 } 75