Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -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; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.LayoutInflater; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | import android.widget.AdapterView; |
| 25 | import android.widget.BaseAdapter; |
| 26 | import android.widget.Spinner; |
| 27 | import android.widget.TextView; |
| 28 | |
| 29 | import com.android.documentsui.NavigationViewManager.Breadcrumb; |
| 30 | import com.android.documentsui.NavigationViewManager.Environment; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 31 | import com.android.documentsui.base.DocumentInfo; |
| 32 | import com.android.documentsui.base.RootInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 33 | import com.android.documentsui.base.State; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 34 | |
Ben Lin | 6244245 | 2017-01-19 15:10:49 -0800 | [diff] [blame^] | 35 | import java.util.function.IntConsumer; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Dropdown implementation of breadcrumb used for phone device layouts |
| 39 | */ |
| 40 | |
| 41 | public 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 Lin | 6244245 | 2017-01-19 15:10:49 -0800 | [diff] [blame^] | 63 | public void setup(Environment env, State state, IntConsumer listener) { |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 64 | 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 Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 110 | return mState.stack.get(position); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 111 | } |
| 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 | } |