aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/filemanager/controllers/NavigationDrawerController.java
blob: 14381de71b004e095f473981432305a4de2fcaf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2015 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cyanogenmod.filemanager.controllers;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Environment;
import android.os.storage.StorageVolume;
import android.support.design.widget.NavigationView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.cyanogenmod.filemanager.FileManagerApplication;
import com.cyanogenmod.filemanager.R;
import com.cyanogenmod.filemanager.activities.MainActivity;
import com.cyanogenmod.filemanager.adapters.NavigationDrawerAdapter;
import com.cyanogenmod.filemanager.console.VirtualMountPointConsole;
import com.cyanogenmod.filemanager.model.Bookmark;
import com.cyanogenmod.filemanager.model.Bookmark.BOOKMARK_TYPE;
import com.cyanogenmod.filemanager.model.MountPoint;
import com.cyanogenmod.filemanager.model.NavigationDrawerItem;
import com.cyanogenmod.filemanager.model.NavigationDrawerItem.NavigationDrawerItemType;
import com.cyanogenmod.filemanager.preferences.AccessMode;
import com.cyanogenmod.filemanager.util.FileHelper;
import com.cyanogenmod.filemanager.util.StorageHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.cyanogenmod.filemanager.model.Bookmark.BOOKMARK_TYPE.SDCARD;
import static com.cyanogenmod.filemanager.model.Bookmark.BOOKMARK_TYPE.USB;

/**
 * NavigationDrawerController. This class is contains logic to add/remove and manage items in
 * the NavigationDrawer which uses android support libraries NavigationView.
 */
public class NavigationDrawerController {
    private static final String TAG = NavigationDrawerController.class.getSimpleName();
    private static boolean DEBUG = false;

    private static final String STR_USB = "usb"; // $NON-NLS-1$

    private Context mCtx;
    private NavigationView mNavigationDrawer;
    private NavigationDrawerAdapter mAdapter;
    private List<NavigationDrawerItem> mNavigationDrawerItemList;
    private int mLastRoot;
    private int mCurrentSelection;
    private Map<Integer, Bookmark> mStorageBookmarks;

    public NavigationDrawerController(Context ctx, NavigationView navigationView) {
        mCtx = ctx;
        mNavigationDrawer = navigationView;
        mStorageBookmarks = new HashMap<Integer, Bookmark>();
        mNavigationDrawerItemList = new ArrayList<NavigationDrawerItem>();
        mLastRoot = 0;
        mCurrentSelection = -1;
        ListView listView = (ListView)mNavigationDrawer.findViewById(R.id.navigation_view_listview);
        listView.setOnItemClickListener(((MainActivity)mCtx));
        mAdapter = new NavigationDrawerAdapter(mCtx, mNavigationDrawerItemList);
        listView.setAdapter(mAdapter);
    }

    public void loadNavigationDrawerItems() {
        // clear current special nav drawer items
        removeAllItemsFromDrawer();

        mLastRoot = 0;
        String title = null;
        String summary = null;
        int color;

        // Determine display mode
        boolean showRoot = FileManagerApplication.getAccessMode().compareTo(AccessMode.SAFE) != 0;
        NavigationDrawerItemType itemType = showRoot ?
                NavigationDrawerItemType.DOUBLE : NavigationDrawerItemType.SINGLE;

        // Load Header
        mNavigationDrawerItemList.add(new NavigationDrawerItem(0, NavigationDrawerItemType.HEADER,
                null, null, 0, 0));

        // Load Home and Favorites
        title = mCtx.getResources().getString(R.string.navigation_item_title_home);
        summary = null;
        color = mCtx.getResources().getColor(R.color.default_primary);
        mNavigationDrawerItemList.add(new NavigationDrawerItem(R.id.navigation_item_home,
                NavigationDrawerItemType.SINGLE, title, summary, R.drawable.ic_home, color));
        // TODO: Re-enable Favorites once we have a fragment for it
        //title = mCtx.getResources().getString(R.string.navigation_item_title_favorites);
        //summary = null;
        //color = mCtx.getResources().getColor(R.color.favorites_primary);
        //mNavigationDrawerItemList.add(new NavigationDrawerItem(R.id.navigation_item_favorites,
        //    NavigationDrawerItemType.SINGLE, title, summary, R.drawable.ic_favorite_on, color));

        // Divider
        mNavigationDrawerItemList.add(new NavigationDrawerItem(0, NavigationDrawerItemType.DIVIDER,
                null, null, 0, 0));

        // Load Local Storage
        title = mCtx.getResources().getString(R.string.navigation_item_title_local);
        summary = StorageHelper.getLocalStoragePath(mCtx);
        color = mCtx.getResources().getColor(R.color.default_primary);
        mNavigationDrawerItemList.add(new NavigationDrawerItem(R.id.navigation_item_internal,
                itemType, title, summary, R.drawable.ic_source_internal, color));

        // Show/hide root
        if (showRoot) {
            title = mCtx.getResources().getString(R.string.navigation_item_title_root);
            summary = FileHelper.ROOT_DIRECTORY;
            color = mCtx.getResources().getColor(R.color.root_primary);
            mNavigationDrawerItemList.add(new NavigationDrawerItem(R.id.navigation_item_root_d,
                    itemType, title, summary, R.drawable.ic_source_root_d, color));
        }

        loadExternalStorageItems(itemType);

        // Grab storageapi providers insertion spot in list.
        mLastRoot = mNavigationDrawerItemList.size();

        loadSecureStorage();

        // Divider
        mNavigationDrawerItemList.add(new NavigationDrawerItem(0, NavigationDrawerItemType.DIVIDER,
                null, null, 0, 0));

        // Load manage storage and settings
        title = mCtx.getResources().getString(R.string.navigation_item_title_manage);
        summary = null;
        color = mCtx.getResources().getColor(R.color.misc_primary);
        mNavigationDrawerItemList.add(new NavigationDrawerItem(R.id.navigation_item_manage,
                NavigationDrawerItemType.SINGLE, title, summary, R.drawable.ic_storage_sources,
                color));
        title = mCtx.getResources().getString(R.string.navigation_item_title_settings);
        summary = null;
        color = mCtx.getResources().getColor(R.color.misc_primary);
        mNavigationDrawerItemList.add(new NavigationDrawerItem(R.id.navigation_item_settings,
                NavigationDrawerItemType.SINGLE, title, summary, R.drawable.ic_settings, color));

        // Notify dataset changed here because we aren't sure when/if storage providers will return.
        mAdapter.notifyDataSetChanged();

    }

    /**
     * Method that loads the secure digital card and usb storage menu items from the system.
     */
    private void loadExternalStorageItems(NavigationDrawerItemType itemType) {
        List<Bookmark> sdBookmarks = new ArrayList<Bookmark>();
        List<Bookmark> usbBookmarks = new ArrayList<Bookmark>();

        try {
            // Recovery sdcards and usb from storage manager
            StorageVolume[] volumes =
                    StorageHelper.getStorageVolumes(mCtx, true);
            for (StorageVolume volume: volumes) {
                if (volume != null) {
                    String mountedState = volume.getState();
                    String path = volume.getPath();
                    if (!Environment.MEDIA_MOUNTED.equalsIgnoreCase(mountedState) &&
                            !Environment.MEDIA_MOUNTED_READ_ONLY.equalsIgnoreCase(mountedState)) {
                        if (DEBUG) {
                            Log.w(TAG, "Ignoring '" + path + "' with state of '"
                                    + mountedState + "'");
                        }
                        continue;
                    }
                    if (!TextUtils.isEmpty(path)) {
                        String lowerPath = path.toLowerCase(Locale.ROOT);
                        Bookmark bookmark;
                        if (lowerPath.contains(STR_USB)) {
                            usbBookmarks.add(new Bookmark(USB, StorageHelper
                                    .getStorageVolumeDescription(mCtx,
                                            volume), path));
                        } else {
                            sdBookmarks.add(new Bookmark(SDCARD, StorageHelper
                                    .getStorageVolumeDescription(mCtx,
                                            volume), path));
                        }
                    }
                }
            }

            String localStorage = mCtx.getResources().getString(R.string.local_storage_path);

            // Load the bookmarks
            for (Bookmark b : sdBookmarks) {
                if (TextUtils.equals(b.getPath(), localStorage)) continue;
                int hash = b.hashCode();
                int color = mCtx.getResources().getColor(R.color.sdcard_primary);
                mNavigationDrawerItemList.add(new NavigationDrawerItem(hash, itemType, b.getName(),
                        b.getPath(), R.drawable.ic_source_sd_card, color));
                mStorageBookmarks.put(hash, b);
            }
            for (Bookmark b : usbBookmarks) {
                int hash = b.hashCode();
                int color = mCtx.getResources().getColor(R.color.usb_primary);
                mNavigationDrawerItemList.add(new NavigationDrawerItem(hash, itemType, b.getName(),
                        b.getPath(), R.drawable.ic_source_usb, color));
                mStorageBookmarks.put(hash, b);
            }
        }
        catch (Throwable ex) {
            Log.e(TAG, "Load filesystem bookmarks failed", ex); //$NON-NLS-1$
        }
    }

    /**
     * Method that loads the secure storage mount point.
     */
    private void loadSecureStorage() {
        List<MountPoint> mps = VirtualMountPointConsole.getVirtualMountPoints();
        for (MountPoint mp : mps) {
            BOOKMARK_TYPE type = null;
            String name = null;
            if (mp.isSecure()) {
                type = BOOKMARK_TYPE.SECURE;
                name = mCtx.getResources().getString(R.string.navigation_item_title_protected);
                Bookmark b = new Bookmark(type, name, mp.getMountPoint());

                int hash = b.hashCode();
                int color = mCtx.getResources().getColor(R.color.protected_primary);
                mNavigationDrawerItemList.add(new NavigationDrawerItem(hash,
                        NavigationDrawerItemType.SINGLE, b.getName(), b.getPath(),
                        R.drawable.ic_source_protected, color));
                mStorageBookmarks.put(hash, b);
                break;
            } else {
                continue;
            }
        }
    }

    public void removeAllItemsFromDrawer() {
        // reset menu list
        mNavigationDrawerItemList.clear();

        // reset hashmaps
        mStorageBookmarks.clear();
    }

    public Bookmark getBookmarkFromMenuItem(int key) {
        return mStorageBookmarks.get(key);
    }

    public void setSelected(int position) {
        // Unset old selection
        if (mCurrentSelection >= 0 && mCurrentSelection < mNavigationDrawerItemList.size()) {
            NavigationDrawerItem item = mNavigationDrawerItemList.get(mCurrentSelection);
            item.setSelected(false);
        }

        // Set new selection
        if (position > 0 && position < mNavigationDrawerItemList.size()) {
            NavigationDrawerItem item = mNavigationDrawerItemList.get(position);
            item.setSelected(true);
        }

        mCurrentSelection = position;
        mAdapter.notifyDataSetChanged();
    }
}