blob: 97ec89f25f4435dc5378c41f0851caff22f1e8f5 [file] [log] [blame]
Garfield Tan7d75f7b2016-09-20 16:33:24 -07001/*
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.documentsui.sidebar;
18
Ben Lin8e912582016-09-23 14:25:14 -070019import android.annotation.Nullable;
Garfield Tan7d75f7b2016-09-20 16:33:24 -070020import android.content.ClipData;
21import android.content.Context;
22import android.provider.DocumentsProvider;
23import android.text.TextUtils;
24import android.text.format.Formatter;
25import android.view.Menu;
26import android.view.MenuInflater;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.ImageView;
30import android.widget.TextView;
31
Garfield Tanb285b402016-09-21 17:12:18 -070032import com.android.documentsui.ActionHandler;
Garfield Tan7d75f7b2016-09-20 16:33:24 -070033import com.android.documentsui.MenuManager;
34import com.android.documentsui.R;
Ben Lin8e912582016-09-23 14:25:14 -070035import com.android.documentsui.base.DocumentInfo;
Garfield Tan7d75f7b2016-09-20 16:33:24 -070036import com.android.documentsui.base.RootInfo;
37
38/**
39 * An {@link Item} for each root provided by {@link DocumentsProvider}s.
40 */
41class RootItem extends Item {
42 private static final String STRING_ID_FORMAT = "RootItem{%s/%s}";
43
44 public final RootInfo root;
Ben Lin8e912582016-09-23 14:25:14 -070045 public @Nullable DocumentInfo docInfo;
Garfield Tan7d75f7b2016-09-20 16:33:24 -070046
Steve McKay739f94b2016-09-22 14:54:23 -070047 private final ActionHandler mActionHandler;
Garfield Tan7d75f7b2016-09-20 16:33:24 -070048
Steve McKay739f94b2016-09-22 14:54:23 -070049 public RootItem(RootInfo root, ActionHandler actionHandler) {
Garfield Tan7d75f7b2016-09-20 16:33:24 -070050 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() {
100 return root.supportsCreate() && !root.isLibrary();
101 }
102
103 @Override
104 void open() {
Steve McKay6d20d192016-09-21 17:57:10 -0700105 mActionHandler.openRoot(root);
Garfield Tan7d75f7b2016-09-20 16:33:24 -0700106 }
107
108 @Override
Garfield Tanb285b402016-09-21 17:12:18 -0700109 boolean dropOn(ClipData data) {
110 return mActionHandler.dropOn(data, root);
Garfield Tan7d75f7b2016-09-20 16:33:24 -0700111 }
112
113 @Override
114 void createContextMenu(Menu menu, MenuInflater inflater, MenuManager menuManager) {
115 inflater.inflate(R.menu.root_context_menu, menu);
Ben Lin8e912582016-09-23 14:25:14 -0700116 menuManager.updateRootContextMenu(menu, root, docInfo);
Garfield Tan7d75f7b2016-09-20 16:33:24 -0700117 }
118}