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 19 import android.app.Activity; 20 import android.os.Looper; 21 import android.view.View; 22 import android.view.View.OnDragListener; 23 import android.view.ViewGroup; 24 import android.widget.ArrayAdapter; 25 26 import com.android.documentsui.R; 27 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31 32 /** 33 * The adapter for the {@link android.widget.ListView} in sidebar. It contains the list of 34 * {@link Item}s and provides sub-views to {@link android.widget.ListView}. 35 */ 36 class RootsAdapter extends ArrayAdapter<Item> { 37 private static final int TYPE_ROOT = 0; 38 private static final int TYPE_APP = 1; 39 private static final int TYPE_ROOT_AND_APP = 2; 40 private static final int TYPE_SPACER = 3; 41 42 private static final Map<String, Long> sIdMap = new HashMap<>(); 43 // the next available id to associate with a new string id 44 private static long sNextAvailableId; 45 46 private final OnDragListener mDragListener; 47 RootsAdapter( Activity activity, List<Item> items, OnDragListener dragListener)48 public RootsAdapter( 49 Activity activity, 50 List<Item> items, 51 OnDragListener dragListener) { 52 super(activity, 0, items); 53 54 mDragListener = dragListener; 55 } 56 57 @Override hasStableIds()58 public boolean hasStableIds() { 59 return true; 60 } 61 62 @Override getItemId(int position)63 public long getItemId(int position) { 64 // Ensure this method is only called in main thread because we don't have any 65 // concurrency protection. 66 assert(Looper.myLooper() == Looper.getMainLooper()); 67 68 String stringId = getItem(position).stringId; 69 70 long id; 71 if (sIdMap.containsKey(stringId)) { 72 id = sIdMap.get(stringId); 73 } else { 74 id = sNextAvailableId++; 75 sIdMap.put(stringId, id); 76 } 77 78 return id; 79 } 80 81 @Override getView(int position, View convertView, ViewGroup parent)82 public View getView(int position, View convertView, ViewGroup parent) { 83 final Item item = getItem(position); 84 final View view = item.getView(convertView, parent); 85 86 if (item.isRoot()) { 87 view.setTag(R.id.item_position_tag, position); 88 view.setOnDragListener(mDragListener); 89 } else { 90 view.setTag(R.id.item_position_tag, null); 91 view.setOnDragListener(null); 92 } 93 return view; 94 } 95 96 @Override areAllItemsEnabled()97 public boolean areAllItemsEnabled() { 98 return false; 99 } 100 101 @Override isEnabled(int position)102 public boolean isEnabled(int position) { 103 return getItemViewType(position) != TYPE_SPACER; 104 } 105 106 @Override getItemViewType(int position)107 public int getItemViewType(int position) { 108 final Item item = getItem(position); 109 if (item instanceof RootAndAppItem) { 110 return TYPE_ROOT_AND_APP; 111 } else if (item instanceof RootItem) { 112 return TYPE_ROOT; 113 } else if (item instanceof AppItem) { 114 return TYPE_APP; 115 } else { 116 return TYPE_SPACER; 117 } 118 } 119 120 @Override getViewTypeCount()121 public int getViewTypeCount() { 122 return 4; 123 } 124 } 125