Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.documentsui.sidebar; |
| 18 | |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.provider.DocumentsProvider; |
| 22 | import android.text.TextUtils; |
| 23 | import android.text.format.Formatter; |
Ben Lin | 174fc2e | 2017-03-01 17:53:20 -0800 | [diff] [blame] | 24 | import android.view.DragEvent; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 25 | import android.view.Menu; |
| 26 | import android.view.MenuInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.widget.ImageView; |
| 30 | import android.widget.TextView; |
| 31 | |
Garfield Tan | b285b40 | 2016-09-21 17:12:18 -0700 | [diff] [blame] | 32 | import com.android.documentsui.ActionHandler; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 33 | import com.android.documentsui.MenuManager; |
| 34 | import com.android.documentsui.R; |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 35 | import com.android.documentsui.base.DocumentInfo; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 36 | import com.android.documentsui.base.RootInfo; |
| 37 | |
| 38 | /** |
| 39 | * An {@link Item} for each root provided by {@link DocumentsProvider}s. |
| 40 | */ |
| 41 | class RootItem extends Item { |
| 42 | private static final String STRING_ID_FORMAT = "RootItem{%s/%s}"; |
| 43 | |
| 44 | public final RootInfo root; |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 45 | public @Nullable DocumentInfo docInfo; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 46 | |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 47 | private final ActionHandler mActionHandler; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 48 | |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 49 | public RootItem(RootInfo root, ActionHandler actionHandler) { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 50 | super(R.layout.item_root, getStringId(root)); |
| 51 | this.root = root; |
| 52 | mActionHandler = actionHandler; |
| 53 | } |
| 54 | |
| 55 | private static String getStringId(RootInfo root) { |
| 56 | // Empty URI authority is invalid, so we can use empty string if root.authority is null. |
| 57 | // Directly passing null to String.format() will write "null" which can be a valid URI |
| 58 | // authority. |
| 59 | String authority = (root.authority == null ? "" : root.authority); |
| 60 | return String.format(STRING_ID_FORMAT, authority, root.rootId); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void bindView(View convertView) { |
| 65 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 66 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 67 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 68 | final ImageView ejectIcon = (ImageView) convertView.findViewById(R.id.eject_icon); |
| 69 | |
| 70 | final Context context = convertView.getContext(); |
| 71 | icon.setImageDrawable(root.loadDrawerIcon(context)); |
| 72 | title.setText(root.title); |
| 73 | |
| 74 | if (root.supportsEject()) { |
| 75 | ejectIcon.setVisibility(View.VISIBLE); |
| 76 | ejectIcon.setImageDrawable(root.loadEjectIcon(context)); |
| 77 | ejectIcon.setOnClickListener(new OnClickListener() { |
| 78 | @Override |
| 79 | public void onClick(View unmountIcon) { |
| 80 | RootsFragment.ejectClicked(unmountIcon, root, mActionHandler); |
| 81 | } |
| 82 | }); |
| 83 | } else { |
| 84 | ejectIcon.setVisibility(View.GONE); |
| 85 | ejectIcon.setOnClickListener(null); |
| 86 | } |
| 87 | // Show available space if no summary |
| 88 | String summaryText = root.summary; |
| 89 | if (TextUtils.isEmpty(summaryText) && root.availableBytes >= 0) { |
| 90 | summaryText = context.getString(R.string.root_available_bytes, |
| 91 | Formatter.formatFileSize(context, root.availableBytes)); |
| 92 | } |
| 93 | |
| 94 | summary.setText(summaryText); |
| 95 | summary.setVisibility(TextUtils.isEmpty(summaryText) ? View.GONE : View.VISIBLE); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | boolean isDropTarget() { |
Ben Lin | d6a85b9 | 2017-03-13 16:26:26 -0700 | [diff] [blame] | 100 | return true; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | void open() { |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 105 | mActionHandler.openRoot(root); |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Ben Lin | 174fc2e | 2017-03-01 17:53:20 -0800 | [diff] [blame] | 109 | boolean dropOn(DragEvent event) { |
| 110 | return mActionHandler.dropOn(event, root); |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | void createContextMenu(Menu menu, MenuInflater inflater, MenuManager menuManager) { |
| 115 | inflater.inflate(R.menu.root_context_menu, menu); |
Ben Lin | 8e91258 | 2016-09-23 14:25:14 -0700 | [diff] [blame] | 116 | menuManager.updateRootContextMenu(menu, root, docInfo); |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 117 | } |
| 118 | } |