blob: 7f5e5f36eb2bcd26a5d494fea7c86ec806e18851 [file] [log] [blame]
Ben Linb8c54e72016-06-10 12:13:27 -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;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.AdapterView;
25import android.widget.BaseAdapter;
26import android.widget.Spinner;
27import android.widget.TextView;
28
29import com.android.documentsui.NavigationViewManager.Breadcrumb;
30import com.android.documentsui.NavigationViewManager.Environment;
Steve McKayd0805062016-09-15 14:30:38 -070031import com.android.documentsui.base.DocumentInfo;
32import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070033import com.android.documentsui.base.State;
Ben Linb8c54e72016-06-10 12:13:27 -070034
Ben Lin62442452017-01-19 15:10:49 -080035import java.util.function.IntConsumer;
Ben Linb8c54e72016-06-10 12:13:27 -070036
37/**
38 * Dropdown implementation of breadcrumb used for phone device layouts
39 */
40
41public final class DropdownBreadcrumb extends Spinner implements Breadcrumb {
42
43 private DropdownAdapter mAdapter;
44
45 public DropdownBreadcrumb(
46 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
47 super(context, attrs, defStyleAttr, defStyleRes);
48 }
49
50 public DropdownBreadcrumb(Context context, AttributeSet attrs, int defStyleAttr) {
51 super(context, attrs, defStyleAttr);
52 }
53
54 public DropdownBreadcrumb(Context context, AttributeSet attrs) {
55 super(context, attrs);
56 }
57
58 public DropdownBreadcrumb(Context context) {
59 super(context);
60 }
61
62 @Override
Ben Lin62442452017-01-19 15:10:49 -080063 public void setup(Environment env, State state, IntConsumer listener) {
Ben Linb8c54e72016-06-10 12:13:27 -070064 mAdapter = new DropdownAdapter(state, env);
65 setOnItemSelectedListener(
66 new OnItemSelectedListener() {
67 @Override
68 public void onItemSelected(
69 AdapterView<?> parent, View view, int position, long id) {
70 listener.accept(position);
71 }
72
73 @Override
74 public void onNothingSelected(AdapterView<?> parent) {}
75 });
76 }
77
78 @Override
79 public void show(boolean visibility) {
80 if (visibility) {
81 setVisibility(VISIBLE);
82 setAdapter(mAdapter);
83 } else {
84 setVisibility(GONE);
85 setAdapter(null);
86 }
87 }
88
89 @Override
90 public void postUpdate() {
91 setSelection(mAdapter.getCount() - 1, false);
92 }
93
94 private static final class DropdownAdapter extends BaseAdapter {
95 private Environment mEnv;
96 private State mState;
97
98 public DropdownAdapter(State state, Environment env) {
99 mState = state;
100 mEnv = env;
101 }
102
103 @Override
104 public int getCount() {
105 return mState.stack.size();
106 }
107
108 @Override
109 public DocumentInfo getItem(int position) {
Garfield Tan2a837422016-10-19 11:50:45 -0700110 return mState.stack.get(position);
Ben Linb8c54e72016-06-10 12:13:27 -0700111 }
112
113 @Override
114 public long getItemId(int position) {
115 return position;
116 }
117
118 @Override
119 public View getView(int position, View convertView, ViewGroup parent) {
120 if (convertView == null) {
121 convertView = LayoutInflater.from(parent.getContext())
122 .inflate(R.layout.item_subdir_title, parent, false);
123 }
124
125 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
126 final DocumentInfo doc = getItem(position);
127
128 if (position == 0) {
129 final RootInfo root = mEnv.getCurrentRoot();
130 title.setText(root.title);
131 } else {
132 title.setText(doc.displayName);
133 }
134
135 return convertView;
136 }
137
138 @Override
139 public View getDropDownView(int position, View convertView, ViewGroup parent) {
140 if (convertView == null) {
141 convertView = LayoutInflater.from(parent.getContext())
142 .inflate(R.layout.item_subdir, parent, false);
143 }
144
145 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
146 final DocumentInfo doc = getItem(position);
147
148 if (position == 0) {
149 final RootInfo root = mEnv.getCurrentRoot();
150 title.setText(root.title);
151 } else {
152 title.setText(doc.displayName);
153 }
154
155 return convertView;
156 }
157 }
158
159}