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 package com.google.android.car.kitchensink.storagevolumes;
17 
18 import android.annotation.Nullable;
19 import android.database.ContentObserver;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.os.storage.StorageManager;
24 import android.os.storage.VolumeInfo;
25 import android.provider.MediaStore;
26 import android.util.Log;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ArrayAdapter;
31 import android.widget.ListView;
32 
33 import androidx.annotation.NonNull;
34 import androidx.fragment.app.Fragment;
35 
36 import com.google.android.car.kitchensink.R;
37 
38 import java.util.List;
39 import java.util.Objects;
40 
41 /**
42  * Listens for changes in content and displays updated list of volumes
43  */
44 public class StorageVolumesFragment extends Fragment {
45     private static final String TAG = "CAR.STORAGEVOLUMES.KS";
46 
47     private ListView mStorageVolumesList;
48     private StorageManager mStorageManager;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         mStorageManager = Objects.requireNonNull(getContext())
53                 .getSystemService(StorageManager.class);
54 
55         super.onCreate(savedInstanceState);
56     }
57 
58     @Nullable
59     @Override
onCreateView( @onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)60     public View onCreateView(
61             @NonNull LayoutInflater inflater,
62             @Nullable ViewGroup container,
63             @Nullable Bundle savedInstanceState) {
64         mStorageVolumesList = (ListView) inflater.inflate(R.layout.storagevolumes,
65                 container, false);
66         return mStorageVolumesList;
67     }
68 
69     @Override
onResume()70     public void onResume() {
71         super.onResume();
72         refresh();
73         Objects.requireNonNull(getContext()).getContentResolver()
74                 .registerContentObserver(MediaStore.AUTHORITY_URI, true, mContentObserver);
75     }
76 
77     @Override
onPause()78     public void onPause() {
79         Objects.requireNonNull(getContext()).getContentResolver()
80                 .unregisterContentObserver(mContentObserver);
81         super.onPause();
82     }
83 
refresh()84     private void refresh() {
85         final List<VolumeInfo> volumes = mStorageManager.getVolumes();
86         volumes.sort(VolumeInfo.getDescriptionComparator());
87 
88         mStorageVolumesList.setAdapter(new ArrayAdapter<>(getContext(),
89                 android.R.layout.simple_list_item_1, volumes.toArray()));
90     }
91 
92     private final ContentObserver mContentObserver = new ContentObserver(new Handler()) {
93         @Override
94         public void onChange(boolean selfChange, Uri uri) {
95             super.onChange(selfChange, uri);
96             if (isResumed()) {
97                 Log.d(TAG, "Content has changed for URI " + uri);
98                 refresh();
99             }
100         }
101     };
102 }
103