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