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 17package com.android.settings.applications; 18 19import android.app.Fragment; 20import android.content.Context; 21import android.content.Intent; 22import android.graphics.drawable.InsetDrawable; 23import android.os.UserHandle; 24import android.provider.DocumentsContract; 25import android.support.annotation.WorkerThread; 26import android.text.format.Formatter; 27import android.util.Log; 28 29import com.android.settings.R; 30import com.android.settings.Utils; 31import com.android.settingslib.applications.StorageStatsSource; 32 33import java.io.IOException; 34 35/** 36 * MusicViewHolderController controls an Audio/Music file view in the ManageApplications view. 37 */ 38public class MusicViewHolderController implements FileViewHolderController { 39 private static final String TAG = "MusicViewHolderController"; 40 41 private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents"; 42 private static final int INSET_SIZE = 24; // dp 43 44 private Context mContext; 45 private StorageStatsSource mSource; 46 private String mVolumeUuid; 47 private long mMusicSize; 48 private UserHandle mUser; 49 50 public MusicViewHolderController( 51 Context context, StorageStatsSource source, String volumeUuid, UserHandle user) { 52 mContext = context; 53 mSource = source; 54 mVolumeUuid = volumeUuid; 55 mUser = user; 56 } 57 58 @Override 59 @WorkerThread 60 public void queryStats() { 61 try { 62 mMusicSize = mSource.getExternalStorageStats(mVolumeUuid, mUser).audioBytes; 63 } catch (IOException e) { 64 mMusicSize = 0; 65 Log.w(TAG, e); 66 } 67 } 68 69 @Override 70 public boolean shouldShow() { 71 return true; 72 } 73 74 @Override 75 public void setupView(AppViewHolder holder) { 76 holder.appIcon.setImageDrawable( 77 new InsetDrawable(mContext.getDrawable(R.drawable.ic_headset_24dp), INSET_SIZE)); 78 holder.appName.setText(mContext.getText(R.string.audio_files_title)); 79 holder.summary.setText(Formatter.formatFileSize(mContext, mMusicSize)); 80 } 81 82 @Override 83 public void onClick(Fragment fragment) { 84 Intent intent = new Intent(Intent.ACTION_VIEW); 85 intent.setDataAndType( 86 DocumentsContract.buildRootUri(AUTHORITY_MEDIA, "audio_root"), 87 DocumentsContract.Root.MIME_TYPE_ITEM); 88 intent.addCategory(Intent.CATEGORY_DEFAULT); 89 intent.putExtra(Intent.EXTRA_USER_ID, mUser); 90 Utils.launchIntent(fragment, intent); 91 } 92} 93