diff options
author | Trevor Johns <trevorjohns@google.com> | 2013-10-30 16:38:01 -0700 |
---|---|---|
committer | Trevor Johns <trevorjohns@google.com> | 2013-10-30 16:38:01 -0700 |
commit | a6b4636faeaa3613bae3dfcbed7fd8886d615714 (patch) | |
tree | 1d5208749f6fcad3523f88da60ab48294c264ce0 /common | |
parent | 27162ffddab31cb4d7f4eb1177c5fe7fc48875a9 (diff) | |
download | android-a6b4636faeaa3613bae3dfcbed7fd8886d615714.tar.gz |
Restore "Merge downstream branch 'developers-dev' into 'klp-dev'"
This reverts commit ec985147ec781dfff9a229c6b794ee4eac0ced91.
Diffstat (limited to 'common')
16 files changed, 1667 insertions, 10 deletions
diff --git a/common/src/com/example/android/common/actionbarcompat/MultiSelectionUtil.java b/common/src/com/example/android/common/actionbarcompat/MultiSelectionUtil.java new file mode 100644 index 00000000..482f6edf --- /dev/null +++ b/common/src/com/example/android/common/actionbarcompat/MultiSelectionUtil.java @@ -0,0 +1,293 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.common.actionbarcompat; + +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.support.v7.view.ActionMode; +import android.util.Pair; +import android.util.SparseBooleanArray; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.AbsListView; +import android.widget.Adapter; +import android.widget.AdapterView; +import android.widget.ListView; + +import java.util.HashSet; + +/** + * Utilities for handling multiple selection in list views. Contains functionality similar to {@link + * AbsListView#CHOICE_MODE_MULTIPLE_MODAL} which works with {@link ActionBarActivity} and + * backward-compatible action bars. + */ +public class MultiSelectionUtil { + + /** + * Attach a Controller to the given <code>listView</code>, <code>activity</code> + * and <code>listener</code>. + * + * @param listView ListView which displays {@link android.widget.Checkable} items. + * @param activity Activity which contains the ListView. + * @param listener Listener that will manage the selection mode. + * @return the attached Controller instance. + */ + public static Controller attachMultiSelectionController(final ListView listView, + final ActionBarActivity activity, final MultiChoiceModeListener listener) { + return new Controller(listView, activity, listener); + } + + /** + * Class which provides functionality similar to {@link AbsListView#CHOICE_MODE_MULTIPLE_MODAL} + * for the {@link ListView} provided to it. A + * {@link android.widget.AdapterView.OnItemLongClickListener} is set on the ListView so that + * when an item is long-clicked an ActionBarCompat Action Mode is started. Once started, a + * {@link android.widget.AdapterView.OnItemClickListener} is set so that an item click toggles + * that item's checked state. + */ + public static class Controller { + + private final ListView mListView; + private final ActionBarActivity mActivity; + private final MultiChoiceModeListener mListener; + private final Callbacks mCallbacks; + + // Current Action Mode (if there is one) + private ActionMode mActionMode; + + // Keeps record of any items that should be checked on the next action mode creation + private HashSet<Pair<Integer, Long>> mItemsToCheck; + + // Reference to the replace OnItemClickListener (so it can be restored later) + private AdapterView.OnItemClickListener mOldItemClickListener; + + private final Runnable mSetChoiceModeNoneRunnable = new Runnable() { + @Override + public void run() { + mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); + } + }; + + private Controller(ListView listView, ActionBarActivity activity, + MultiChoiceModeListener listener) { + mListView = listView; + mActivity = activity; + mListener = listener; + mCallbacks = new Callbacks(); + + // We set ourselves as the OnItemLongClickListener so we know when to start + // an Action Mode + listView.setOnItemLongClickListener(mCallbacks); + } + + /** + * Finish the current Action Mode (if there is one). + */ + public void finish() { + if (mActionMode != null) { + mActionMode.finish(); + } + } + + /** + * This method should be called from your {@link ActionBarActivity} or + * {@link android.support.v4.app.Fragment Fragment} to allow the controller to restore any + * instance state. + * + * @param savedInstanceState - The state passed to your Activity or Fragment. + */ + public void restoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState != null) { + long[] checkedIds = savedInstanceState.getLongArray(getStateKey()); + if (checkedIds != null && checkedIds.length > 0) { + HashSet<Long> idsToCheckOnRestore = new HashSet<Long>(); + for (long id : checkedIds) { + idsToCheckOnRestore.add(id); + } + tryRestoreInstanceState(idsToCheckOnRestore); + } + } + } + + /** + * This method should be called from + * {@link ActionBarActivity#onSaveInstanceState(android.os.Bundle)} or + * {@link android.support.v4.app.Fragment#onSaveInstanceState(android.os.Bundle) + * Fragment.onSaveInstanceState(Bundle)} to allow the controller to save its instance + * state. + * + * @param outState - The state passed to your Activity or Fragment. + */ + public void saveInstanceState(Bundle outState) { + if (mActionMode != null && mListView.getAdapter().hasStableIds()) { + outState.putLongArray(getStateKey(), mListView.getCheckedItemIds()); + } + } + + // Internal utility methods + + private String getStateKey() { + return MultiSelectionUtil.class.getSimpleName() + "_" + mListView.getId(); + } + + private void tryRestoreInstanceState(HashSet<Long> idsToCheckOnRestore) { + if (idsToCheckOnRestore == null || mListView.getAdapter() == null) { + return; + } + + boolean idsFound = false; + Adapter adapter = mListView.getAdapter(); + for (int pos = adapter.getCount() - 1; pos >= 0; pos--) { + if (idsToCheckOnRestore.contains(adapter.getItemId(pos))) { + idsFound = true; + if (mItemsToCheck == null) { + mItemsToCheck = new HashSet<Pair<Integer, Long>>(); + } + mItemsToCheck.add(new Pair<Integer, Long>(pos, adapter.getItemId(pos))); + } + } + + if (idsFound) { + // We found some IDs that were checked. Let's now restore the multi-selection + // state. + mActionMode = mActivity.startSupportActionMode(mCallbacks); + } + } + + /** + * This class encapsulates all of the callbacks necessary for the controller class. + */ + final class Callbacks implements ActionMode.Callback, AdapterView.OnItemClickListener, + AdapterView.OnItemLongClickListener { + + @Override + public final boolean onCreateActionMode(ActionMode actionMode, Menu menu) { + if (mListener.onCreateActionMode(actionMode, menu)) { + mActionMode = actionMode; + // Keep a reference to the existing OnItemClickListener so we can restore it + mOldItemClickListener = mListView.getOnItemClickListener(); + + // Set-up the ListView to emulate CHOICE_MODE_MULTIPLE_MODAL + mListView.setOnItemClickListener(this); + mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); + mListView.removeCallbacks(mSetChoiceModeNoneRunnable); + + // If there are some items to check, do it now + if (mItemsToCheck != null) { + for (Pair<Integer, Long> posAndId : mItemsToCheck) { + mListView.setItemChecked(posAndId.first, true); + // Notify the listener that the item has been checked + mListener.onItemCheckedStateChanged(mActionMode, posAndId.first, + posAndId.second, true); + } + } + return true; + } + return false; + } + + @Override + public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { + // Proxy listener + return mListener.onPrepareActionMode(actionMode, menu); + } + + @Override + public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { + // Proxy listener + return mListener.onActionItemClicked(actionMode, menuItem); + } + + @Override + public void onDestroyActionMode(ActionMode actionMode) { + mListener.onDestroyActionMode(actionMode); + + // Clear all the checked items + SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions(); + if (checkedPositions != null) { + for (int i = 0; i < checkedPositions.size(); i++) { + mListView.setItemChecked(checkedPositions.keyAt(i), false); + } + } + + // Restore the original onItemClickListener + mListView.setOnItemClickListener(mOldItemClickListener); + + // Clear the Action Mode + mActionMode = null; + + // Reset the ListView's Choice Mode + mListView.post(mSetChoiceModeNoneRunnable); + } + + @Override + public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { + // Check to see what the new checked state is, and then notify the listener + final boolean checked = mListView.isItemChecked(position); + mListener.onItemCheckedStateChanged(mActionMode, position, id, checked); + + boolean hasCheckedItem = checked; + + // Check to see if we have any checked items + if (!hasCheckedItem) { + SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions(); + if (checkedItemPositions != null) { + // Iterate through the SparseBooleanArray to see if there is a checked item + int i = 0; + while (!hasCheckedItem && i < checkedItemPositions.size()) { + hasCheckedItem = checkedItemPositions.valueAt(i++); + } + } + } + + // If we don't have any checked items, finish the action mode + if (!hasCheckedItem) { + mActionMode.finish(); + } + } + + @Override + public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, + long id) { + // If we already have an action mode started return false + // (onItemClick will be called anyway) + if (mActionMode != null) { + return false; + } + + mItemsToCheck = new HashSet<Pair<Integer, Long>>(); + mItemsToCheck.add(new Pair<Integer, Long>(position, id)); + mActionMode = mActivity.startSupportActionMode(this); + return true; + } + } + } + + /** + * @see android.widget.AbsListView.MultiChoiceModeListener + */ + public static interface MultiChoiceModeListener extends ActionMode.Callback { + + /** + * @see android.widget.AbsListView.MultiChoiceModeListener#onItemCheckedStateChanged( + *android.view.ActionMode, int, long, boolean) + */ + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, + boolean checked); + } +} diff --git a/common/src/com/example/android/common/dummydata/Cheeses.java b/common/src/com/example/android/common/dummydata/Cheeses.java new file mode 100644 index 00000000..a386e68e --- /dev/null +++ b/common/src/com/example/android/common/dummydata/Cheeses.java @@ -0,0 +1,165 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.common.dummydata; + +import java.util.ArrayList; + +/** + * Dummy data. + */ +public class Cheeses { + static final String[] CHEESES = { + "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", + "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", + "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese", + "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell", + "Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc", + "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", + "Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon", + "Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase", + "Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese", + "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase", "Bishop Kennedy", + "Blarney", "Bleu d'Auvergne", "Bleu de Gex", "Bleu de Laqueuille", + "Bleu de Septmoncel", "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore", + "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)", + "Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon", "Boule Du Roves", + "Boulette d'Avesnes", "Boursault", "Boursin", "Bouyssou", "Bra", "Braudostur", + "Breakfast Cheese", "Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon", + "Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun", "Brillat-Savarin", + "Brin", "Brin d' Amour", "Brin d'Amour", "Brinza (Burduf Brinza)", + "Briquette de Brebis", "Briquette du Forez", "Broccio", "Broccio Demi-Affine", + "Brousse du Rove", "Bruder Basil", "Brusselae Kaas (Fromage de Bruxelles)", "Bryndza", + "Buchette d'Anjou", "Buffalo", "Burgos", "Butte", "Butterkase", "Button (Innes)", + "Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille", "Caciocavallo", "Caciotta", + "Caerphilly", "Cairnsmore", "Calenzana", "Cambazola", "Camembert de Normandie", + "Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux", "Capricorn Goat", + "Capriole Banon", "Carre de l'Est", "Casciotta di Urbino", "Cashel Blue", "Castellano", + "Castelleno", "Castelmagno", "Castelo Branco", "Castigliano", "Cathelain", + "Celtic Promise", "Cendre d'Olivet", "Cerney", "Chabichou", "Chabichou du Poitou", + "Chabis de Gatine", "Chaource", "Charolais", "Chaumes", "Cheddar", + "Cheddar Clothbound", "Cheshire", "Chevres", "Chevrotin des Aravis", "Chontaleno", + "Civray", "Coeur de Camembert au Calvados", "Coeur de Chevre", "Colby", "Cold Pack", + "Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper", + "Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)", + "Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese", + "Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza", + "Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley", + "Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino", + "Cypress Grove Chevre", "Danablu (Danish Blue)", "Danbo", "Danish Fontina", + "Daralagjazsky", "Dauphin", "Delice des Fiouves", "Denhany Dorset Drum", "Derby", + "Dessertnyj Belyj", "Devon Blue", "Devon Garland", "Dolcelatte", "Doolin", + "Doppelrhamstufel", "Dorset Blue Vinney", "Double Gloucester", "Double Worcester", + "Dreux a la Feuille", "Dry Jack", "Duddleswell", "Dunbarra", "Dunlop", "Dunsyre Blue", + "Duroblando", "Durrus", "Dutch Mimolette (Commissiekaas)", "Edam", "Edelpilz", + "Emental Grand Cru", "Emlett", "Emmental", "Epoisses de Bourgogne", "Esbareich", + "Esrom", "Etorki", "Evansdale Farmhouse Brie", "Evora De L'Alentejo", "Exmoor Blue", + "Explorateur", "Feta", "Feta (Australian)", "Figue", "Filetta", "Fin-de-Siecle", + "Finlandia Swiss", "Finn", "Fiore Sardo", "Fleur du Maquis", "Flor de Guia", + "Flower Marie", "Folded", "Folded cheese with mint", "Fondant de Brebis", + "Fontainebleau", "Fontal", "Fontina Val d'Aosta", "Formaggio di capra", "Fougerus", + "Four Herb Gouda", "Fourme d' Ambert", "Fourme de Haute Loire", "Fourme de Montbrison", + "Fresh Jack", "Fresh Mozzarella", "Fresh Ricotta", "Fresh Truffles", "Fribourgeois", + "Friesekaas", "Friesian", "Friesla", "Frinault", "Fromage a Raclette", "Fromage Corse", + "Fromage de Montagne de Savoie", "Fromage Frais", "Fruit Cream Cheese", + "Frying Cheese", "Fynbo", "Gabriel", "Galette du Paludier", "Galette Lyonnaise", + "Galloway Goat's Milk Gems", "Gammelost", "Gaperon a l'Ail", "Garrotxa", "Gastanberra", + "Geitost", "Gippsland Blue", "Gjetost", "Gloucester", "Golden Cross", "Gorgonzola", + "Gornyaltajski", "Gospel Green", "Gouda", "Goutu", "Gowrie", "Grabetto", "Graddost", + "Grafton Village Cheddar", "Grana", "Grana Padano", "Grand Vatel", + "Grataron d' Areches", "Gratte-Paille", "Graviera", "Greuilh", "Greve", + "Gris de Lille", "Gruyere", "Gubbeen", "Guerbigny", "Halloumi", + "Halloumy (Australian)", "Haloumi-Style Cheese", "Harbourne Blue", "Havarti", + "Heidi Gruyere", "Hereford Hop", "Herrgardsost", "Herriot Farmhouse", "Herve", + "Hipi Iti", "Hubbardston Blue Cow", "Hushallsost", "Iberico", "Idaho Goatster", + "Idiazabal", "Il Boschetto al Tartufo", "Ile d'Yeu", "Isle of Mull", "Jarlsberg", + "Jermi Tortes", "Jibneh Arabieh", "Jindi Brie", "Jubilee Blue", "Juustoleipa", + "Kadchgall", "Kaseri", "Kashta", "Kefalotyri", "Kenafa", "Kernhem", "Kervella Affine", + "Kikorangi", "King Island Cape Wickham Brie", "King River Gold", "Klosterkaese", + "Knockalara", "Kugelkase", "L'Aveyronnais", "L'Ecir de l'Aubrac", "La Taupiniere", + "La Vache Qui Rit", "Laguiole", "Lairobell", "Lajta", "Lanark Blue", "Lancashire", + "Langres", "Lappi", "Laruns", "Lavistown", "Le Brin", "Le Fium Orbo", "Le Lacandou", + "Le Roule", "Leafield", "Lebbene", "Leerdammer", "Leicester", "Leyden", "Limburger", + "Lincolnshire Poacher", "Lingot Saint Bousquet d'Orb", "Liptauer", "Little Rydings", + "Livarot", "Llanboidy", "Llanglofan Farmhouse", "Loch Arthur Farmhouse", + "Loddiswell Avondale", "Longhorn", "Lou Palou", "Lou Pevre", "Lyonnais", "Maasdam", + "Macconais", "Mahoe Aged Gouda", "Mahon", "Malvern", "Mamirolle", "Manchego", + "Manouri", "Manur", "Marble Cheddar", "Marbled Cheeses", "Maredsous", "Margotin", + "Maribo", "Maroilles", "Mascares", "Mascarpone", "Mascarpone (Australian)", + "Mascarpone Torta", "Matocq", "Maytag Blue", "Meira", "Menallack Farmhouse", + "Menonita", "Meredith Blue", "Mesost", "Metton (Cancoillotte)", "Meyer Vintage Gouda", + "Mihalic Peynir", "Milleens", "Mimolette", "Mine-Gabhar", "Mini Baby Bells", "Mixte", + "Molbo", "Monastery Cheeses", "Mondseer", "Mont D'or Lyonnais", "Montasio", + "Monterey Jack", "Monterey Jack Dry", "Morbier", "Morbier Cru de Montagne", + "Mothais a la Feuille", "Mozzarella", "Mozzarella (Australian)", + "Mozzarella di Bufala", "Mozzarella Fresh, in water", "Mozzarella Rolls", "Munster", + "Murol", "Mycella", "Myzithra", "Naboulsi", "Nantais", "Neufchatel", + "Neufchatel (Australian)", "Niolo", "Nokkelost", "Northumberland", "Oaxaca", + "Olde York", "Olivet au Foin", "Olivet Bleu", "Olivet Cendre", + "Orkney Extra Mature Cheddar", "Orla", "Oschtjepka", "Ossau Fermier", "Ossau-Iraty", + "Oszczypek", "Oxford Blue", "P'tit Berrichon", "Palet de Babligny", "Paneer", "Panela", + "Pannerone", "Pant ys Gawn", "Parmesan (Parmigiano)", "Parmigiano Reggiano", + "Pas de l'Escalette", "Passendale", "Pasteurized Processed", "Pate de Fromage", + "Patefine Fort", "Pave d'Affinois", "Pave d'Auge", "Pave de Chirac", "Pave du Berry", + "Pecorino", "Pecorino in Walnut Leaves", "Pecorino Romano", "Peekskill Pyramid", + "Pelardon des Cevennes", "Pelardon des Corbieres", "Penamellera", "Penbryn", + "Pencarreg", "Perail de Brebis", "Petit Morin", "Petit Pardou", "Petit-Suisse", + "Picodon de Chevre", "Picos de Europa", "Piora", "Pithtviers au Foin", + "Plateau de Herve", "Plymouth Cheese", "Podhalanski", "Poivre d'Ane", "Polkolbin", + "Pont l'Eveque", "Port Nicholson", "Port-Salut", "Postel", "Pouligny-Saint-Pierre", + "Pourly", "Prastost", "Pressato", "Prince-Jean", "Processed Cheddar", "Provolone", + "Provolone (Australian)", "Pyengana Cheddar", "Pyramide", "Quark", + "Quark (Australian)", "Quartirolo Lombardo", "Quatre-Vents", "Quercy Petit", + "Queso Blanco", "Queso Blanco con Frutas --Pina y Mango", "Queso de Murcia", + "Queso del Montsec", "Queso del Tietar", "Queso Fresco", "Queso Fresco (Adobera)", + "Queso Iberico", "Queso Jalapeno", "Queso Majorero", "Queso Media Luna", + "Queso Para Frier", "Queso Quesadilla", "Rabacal", "Raclette", "Ragusano", "Raschera", + "Reblochon", "Red Leicester", "Regal de la Dombes", "Reggianito", "Remedou", + "Requeson", "Richelieu", "Ricotta", "Ricotta (Australian)", "Ricotta Salata", "Ridder", + "Rigotte", "Rocamadour", "Rollot", "Romano", "Romans Part Dieu", "Roncal", "Roquefort", + "Roule", "Rouleau De Beaulieu", "Royalp Tilsit", "Rubens", "Rustinu", "Saaland Pfarr", + "Saanenkaese", "Saga", "Sage Derby", "Sainte Maure", "Saint-Marcellin", + "Saint-Nectaire", "Saint-Paulin", "Salers", "Samso", "San Simon", "Sancerre", + "Sap Sago", "Sardo", "Sardo Egyptian", "Sbrinz", "Scamorza", "Schabzieger", "Schloss", + "Selles sur Cher", "Selva", "Serat", "Seriously Strong Cheddar", "Serra da Estrela", + "Sharpam", "Shelburne Cheddar", "Shropshire Blue", "Siraz", "Sirene", "Smoked Gouda", + "Somerset Brie", "Sonoma Jack", "Sottocenare al Tartufo", "Soumaintrain", + "Sourire Lozerien", "Spenwood", "Sraffordshire Organic", "St. Agur Blue Cheese", + "Stilton", "Stinking Bishop", "String", "Sussex Slipcote", "Sveciaost", "Swaledale", + "Sweet Style Swiss", "Swiss", "Syrian (Armenian String)", "Tala", "Taleggio", "Tamie", + "Tasmania Highland Chevre Log", "Taupiniere", "Teifi", "Telemea", "Testouri", + "Tete de Moine", "Tetilla", "Texas Goat Cheese", "Tibet", "Tillamook Cheddar", + "Tilsit", "Timboon Brie", "Toma", "Tomme Brulee", "Tomme d'Abondance", + "Tomme de Chevre", "Tomme de Romans", "Tomme de Savoie", "Tomme des Chouans", "Tommes", + "Torta del Casar", "Toscanello", "Touree de L'Aubier", "Tourmalet", + "Trappe (Veritable)", "Trois Cornes De Vendee", "Tronchon", "Trou du Cru", "Truffe", + "Tupi", "Turunmaa", "Tymsboro", "Tyn Grug", "Tyning", "Ubriaco", "Ulloa", + "Vacherin-Fribourgeois", "Valencay", "Vasterbottenost", "Venaco", "Vendomois", + "Vieux Corse", "Vignotte", "Vulscombe", "Waimata Farmhouse Blue", + "Washed Rind Cheese (Australian)", "Waterloo", "Weichkaese", "Wellington", + "Wensleydale", "White Stilton", "Whitestone Farmhouse", "Wigmore", "Woodside Cabecou", + "Xanadu", "Xynotyro", "Yarg Cornish", "Yarra Valley Pyramid", "Yorkshire Blue", + "Zamorano", "Zanetti Grana Padano", "Zanetti Parmigiano Reggiano" + }; + + public static ArrayList<String> asList() { + ArrayList<String> items = new ArrayList<String>(); + for (int i = 0, z = CHEESES.length ; i < z ; i++) { + items.add(CHEESES[i]); + } + return items; + } +}
\ No newline at end of file diff --git a/common/src/com/example/android/common/accounts/GenericAccountService.java b/common/src/java/com/example/android/common/accounts/GenericAccountService.java index 9480023b..0cd499a8 100644 --- a/common/src/com/example/android/common/accounts/GenericAccountService.java +++ b/common/src/java/com/example/android/common/accounts/GenericAccountService.java @@ -29,17 +29,23 @@ import android.util.Log; public class GenericAccountService extends Service { private static final String TAG = "GenericAccountService"; - private static final String ACCOUNT_TYPE = "com.example.android.network.sync.basicsyncadapter"; - public static final String ACCOUNT_NAME = "sync"; + public static final String ACCOUNT_NAME = "Account"; private Authenticator mAuthenticator; /** * Obtain a handle to the {@link android.accounts.Account} used for sync in this application. * + * <p>It is important that the accountType specified here matches the value in your sync adapter + * configuration XML file for android.accounts.AccountAuthenticator (often saved in + * res/xml/syncadapter.xml). If this is not set correctly, you'll receive an error indicating + * that "caller uid XXXXX is different than the authenticator's uid". + * + * @param accountType AccountType defined in the configuration XML file for + * android.accounts.AccountAuthenticator (e.g. res/xml/syncadapter.xml). * @return Handle to application's account (not guaranteed to resolve unless CreateSyncAccount() * has been called) */ - public static Account GetAccount() { + public static Account GetAccount(String accountType) { // Note: Normally the account name is set to the user's identity (username or email // address). However, since we aren't actually using any user accounts, it makes more sense // to use a generic string in this case. @@ -47,7 +53,7 @@ public class GenericAccountService extends Service { // This string should *not* be localized. If the user switches locale, we would not be // able to locate the old account, and may erroneously register multiple accounts. final String accountName = ACCOUNT_NAME; - return new Account(accountName, ACCOUNT_TYPE); + return new Account(accountName, accountType); } @Override diff --git a/common/src/java/com/example/android/common/activities/SampleActivityBase.java b/common/src/java/com/example/android/common/activities/SampleActivityBase.java new file mode 100644 index 00000000..3228927b --- /dev/null +++ b/common/src/java/com/example/android/common/activities/SampleActivityBase.java @@ -0,0 +1,52 @@ +/* +* Copyright 2013 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package com.example.android.common.activities; + +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; + +import com.example.android.common.logger.Log; +import com.example.android.common.logger.LogWrapper; + +/** + * Base launcher activity, to handle most of the common plumbing for samples. + */ +public class SampleActivityBase extends FragmentActivity { + + public static final String TAG = "SampleActivityBase"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + protected void onStart() { + super.onStart(); + initializeLogging(); + } + + /** Set up targets to receive log data */ + public void initializeLogging() { + // Using Log, front-end to the logging chain, emulates android.util.log method signatures. + // Wraps Android's native log framework + LogWrapper logWrapper = new LogWrapper(); + Log.setLogNode(logWrapper); + + Log.i(TAG, "Ready"); + } +} diff --git a/common/src/com/example/android/common/db/SelectionBuilder.java b/common/src/java/com/example/android/common/db/SelectionBuilder.java index 51d8cc37..a1964c5f 100644 --- a/common/src/com/example/android/common/db/SelectionBuilder.java +++ b/common/src/java/com/example/android/common/db/SelectionBuilder.java @@ -28,12 +28,10 @@ import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import android.util.Log; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.Map; /** @@ -96,9 +94,9 @@ public class SelectionBuilder { private static final String TAG = "basicsyncadapter"; private String mTable = null; - private Map<String, String> mProjectionMap = Maps.newHashMap(); + private Map<String, String> mProjectionMap = new HashMap<String, String>(); private StringBuilder mSelection = new StringBuilder(); - private ArrayList<String> mSelectionArgs = Lists.newArrayList(); + private ArrayList<String> mSelectionArgs = new ArrayList<String>(); /** * Reset any internal state, allowing this builder to be recycled. diff --git a/common/src/java/com/example/android/common/logger/Log.java b/common/src/java/com/example/android/common/logger/Log.java new file mode 100644 index 00000000..17503c56 --- /dev/null +++ b/common/src/java/com/example/android/common/logger/Log.java @@ -0,0 +1,236 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.common.logger; + +/** + * Helper class for a list (or tree) of LoggerNodes. + * + * <p>When this is set as the head of the list, + * an instance of it can function as a drop-in replacement for {@link android.util.Log}. + * Most of the methods in this class server only to map a method call in Log to its equivalent + * in LogNode.</p> + */ +public class Log { + // Grabbing the native values from Android's native logging facilities, + // to make for easy migration and interop. + public static final int NONE = -1; + public static final int VERBOSE = android.util.Log.VERBOSE; + public static final int DEBUG = android.util.Log.DEBUG; + public static final int INFO = android.util.Log.INFO; + public static final int WARN = android.util.Log.WARN; + public static final int ERROR = android.util.Log.ERROR; + public static final int ASSERT = android.util.Log.ASSERT; + + // Stores the beginning of the LogNode topology. + private static LogNode mLogNode; + + /** + * Returns the next LogNode in the linked list. + */ + public static LogNode getLogNode() { + return mLogNode; + } + + /** + * Sets the LogNode data will be sent to. + */ + public static void setLogNode(LogNode node) { + mLogNode = node; + } + + /** + * Instructs the LogNode to print the log data provided. Other LogNodes can + * be chained to the end of the LogNode as desired. + * + * @param priority Log level of the data being logged. Verbose, Error, etc. + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void println(int priority, String tag, String msg, Throwable tr) { + if (mLogNode != null) { + mLogNode.println(priority, tag, msg, tr); + } + } + + /** + * Instructs the LogNode to print the log data provided. Other LogNodes can + * be chained to the end of the LogNode as desired. + * + * @param priority Log level of the data being logged. Verbose, Error, etc. + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. The actual message to be logged. + */ + public static void println(int priority, String tag, String msg) { + println(priority, tag, msg, null); + } + + /** + * Prints a message at VERBOSE priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void v(String tag, String msg, Throwable tr) { + println(VERBOSE, tag, msg, tr); + } + + /** + * Prints a message at VERBOSE priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + */ + public static void v(String tag, String msg) { + v(tag, msg, null); + } + + + /** + * Prints a message at DEBUG priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void d(String tag, String msg, Throwable tr) { + println(DEBUG, tag, msg, tr); + } + + /** + * Prints a message at DEBUG priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + */ + public static void d(String tag, String msg) { + d(tag, msg, null); + } + + /** + * Prints a message at INFO priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void i(String tag, String msg, Throwable tr) { + println(INFO, tag, msg, tr); + } + + /** + * Prints a message at INFO priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + */ + public static void i(String tag, String msg) { + i(tag, msg, null); + } + + /** + * Prints a message at WARN priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void w(String tag, String msg, Throwable tr) { + println(WARN, tag, msg, tr); + } + + /** + * Prints a message at WARN priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + */ + public static void w(String tag, String msg) { + w(tag, msg, null); + } + + /** + * Prints a message at WARN priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void w(String tag, Throwable tr) { + w(tag, null, tr); + } + + /** + * Prints a message at ERROR priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void e(String tag, String msg, Throwable tr) { + println(ERROR, tag, msg, tr); + } + + /** + * Prints a message at ERROR priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + */ + public static void e(String tag, String msg) { + e(tag, msg, null); + } + + /** + * Prints a message at ASSERT priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void wtf(String tag, String msg, Throwable tr) { + println(ASSERT, tag, msg, tr); + } + + /** + * Prints a message at ASSERT priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. + */ + public static void wtf(String tag, String msg) { + wtf(tag, msg, null); + } + + /** + * Prints a message at ASSERT priority. + * + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public static void wtf(String tag, Throwable tr) { + wtf(tag, null, tr); + } +} diff --git a/common/src/java/com/example/android/common/logger/LogFragment.java b/common/src/java/com/example/android/common/logger/LogFragment.java new file mode 100644 index 00000000..b302acd4 --- /dev/null +++ b/common/src/java/com/example/android/common/logger/LogFragment.java @@ -0,0 +1,109 @@ +/* +* Copyright 2013 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.common.logger; + +import android.graphics.Typeface; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ScrollView; + +/** + * Simple fraggment which contains a LogView and uses is to output log data it receives + * through the LogNode interface. + */ +public class LogFragment extends Fragment { + + private LogView mLogView; + private ScrollView mScrollView; + + public LogFragment() {} + + public View inflateViews() { + mScrollView = new ScrollView(getActivity()); + ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT); + mScrollView.setLayoutParams(scrollParams); + + mLogView = new LogView(getActivity()); + ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams); + logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; + mLogView.setLayoutParams(logParams); + mLogView.setClickable(true); + mLogView.setFocusable(true); + mLogView.setTypeface(Typeface.MONOSPACE); + + // Want to set padding as 16 dips, setPadding takes pixels. Hooray math! + int paddingDips = 16; + double scale = getResources().getDisplayMetrics().density; + int paddingPixels = (int) ((paddingDips * (scale)) + .5); + mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels); + mLogView.setCompoundDrawablePadding(paddingPixels); + + mLogView.setGravity(Gravity.BOTTOM); + mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium); + + mScrollView.addView(mLogView); + return mScrollView; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + View result = inflateViews(); + + mLogView.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) {} + + @Override + public void afterTextChanged(Editable s) { + mScrollView.fullScroll(ScrollView.FOCUS_DOWN); + } + }); + return result; + } + + public LogView getLogView() { + return mLogView; + } +}
\ No newline at end of file diff --git a/common/src/java/com/example/android/common/logger/LogNode.java b/common/src/java/com/example/android/common/logger/LogNode.java new file mode 100644 index 00000000..bc37cabc --- /dev/null +++ b/common/src/java/com/example/android/common/logger/LogNode.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.common.logger; + +/** + * Basic interface for a logging system that can output to one or more targets. + * Note that in addition to classes that will output these logs in some format, + * one can also implement this interface over a filter and insert that in the chain, + * such that no targets further down see certain data, or see manipulated forms of the data. + * You could, for instance, write a "ToHtmlLoggerNode" that just converted all the log data + * it received to HTML and sent it along to the next node in the chain, without printing it + * anywhere. + */ +public interface LogNode { + + /** + * Instructs first LogNode in the list to print the log data provided. + * @param priority Log level of the data being logged. Verbose, Error, etc. + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + public void println(int priority, String tag, String msg, Throwable tr); + +} diff --git a/common/src/java/com/example/android/common/logger/LogView.java b/common/src/java/com/example/android/common/logger/LogView.java new file mode 100644 index 00000000..c01542b9 --- /dev/null +++ b/common/src/java/com/example/android/common/logger/LogView.java @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.common.logger; + +import android.app.Activity; +import android.content.Context; +import android.util.*; +import android.widget.TextView; + +/** Simple TextView which is used to output log data received through the LogNode interface. +*/ +public class LogView extends TextView implements LogNode { + + public LogView(Context context) { + super(context); + } + + public LogView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public LogView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + /** + * Formats the log data and prints it out to the LogView. + * @param priority Log level of the data being logged. Verbose, Error, etc. + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + @Override + public void println(int priority, String tag, String msg, Throwable tr) { + + + String priorityStr = null; + + // For the purposes of this View, we want to print the priority as readable text. + switch(priority) { + case android.util.Log.VERBOSE: + priorityStr = "VERBOSE"; + break; + case android.util.Log.DEBUG: + priorityStr = "DEBUG"; + break; + case android.util.Log.INFO: + priorityStr = "INFO"; + break; + case android.util.Log.WARN: + priorityStr = "WARN"; + break; + case android.util.Log.ERROR: + priorityStr = "ERROR"; + break; + case android.util.Log.ASSERT: + priorityStr = "ASSERT"; + break; + default: + break; + } + + // Handily, the Log class has a facility for converting a stack trace into a usable string. + String exceptionStr = null; + if (tr != null) { + exceptionStr = android.util.Log.getStackTraceString(tr); + } + + // Take the priority, tag, message, and exception, and concatenate as necessary + // into one usable line of text. + final StringBuilder outputBuilder = new StringBuilder(); + + String delimiter = "\t"; + appendIfNotNull(outputBuilder, priorityStr, delimiter); + appendIfNotNull(outputBuilder, tag, delimiter); + appendIfNotNull(outputBuilder, msg, delimiter); + appendIfNotNull(outputBuilder, exceptionStr, delimiter); + + // In case this was originally called from an AsyncTask or some other off-UI thread, + // make sure the update occurs within the UI thread. + ((Activity) getContext()).runOnUiThread( (new Thread(new Runnable() { + @Override + public void run() { + // Display the text we just generated within the LogView. + appendToLog(outputBuilder.toString()); + } + }))); + + if (mNext != null) { + mNext.println(priority, tag, msg, tr); + } + } + + public LogNode getNext() { + return mNext; + } + + public void setNext(LogNode node) { + mNext = node; + } + + /** Takes a string and adds to it, with a separator, if the bit to be added isn't null. Since + * the logger takes so many arguments that might be null, this method helps cut out some of the + * agonizing tedium of writing the same 3 lines over and over. + * @param source StringBuilder containing the text to append to. + * @param addStr The String to append + * @param delimiter The String to separate the source and appended strings. A tab or comma, + * for instance. + * @return The fully concatenated String as a StringBuilder + */ + private StringBuilder appendIfNotNull(StringBuilder source, String addStr, String delimiter) { + if (addStr != null) { + if (addStr.length() == 0) { + delimiter = ""; + } + + return source.append(addStr).append(delimiter); + } + return source; + } + + // The next LogNode in the chain. + LogNode mNext; + + /** Outputs the string as a new line of log data in the LogView. */ + public void appendToLog(String s) { + append("\n" + s); + } + + +} diff --git a/common/src/java/com/example/android/common/logger/LogWrapper.java b/common/src/java/com/example/android/common/logger/LogWrapper.java new file mode 100644 index 00000000..16a9e7ba --- /dev/null +++ b/common/src/java/com/example/android/common/logger/LogWrapper.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.common.logger; + +import android.util.Log; + +/** + * Helper class which wraps Android's native Log utility in the Logger interface. This way + * normal DDMS output can be one of the many targets receiving and outputting logs simultaneously. + */ +public class LogWrapper implements LogNode { + + // For piping: The next node to receive Log data after this one has done its work. + private LogNode mNext; + + /** + * Returns the next LogNode in the linked list. + */ + public LogNode getNext() { + return mNext; + } + + /** + * Sets the LogNode data will be sent to.. + */ + public void setNext(LogNode node) { + mNext = node; + } + + /** + * Prints data out to the console using Android's native log mechanism. + * @param priority Log level of the data being logged. Verbose, Error, etc. + * @param tag Tag for for the log data. Can be used to organize log statements. + * @param msg The actual message to be logged. The actual message to be logged. + * @param tr If an exception was thrown, this can be sent along for the logging facilities + * to extract and print useful information. + */ + @Override + public void println(int priority, String tag, String msg, Throwable tr) { + // There actually are log methods that don't take a msg parameter. For now, + // if that's the case, just convert null to the empty string and move on. + String useMsg = msg; + if (useMsg == null) { + useMsg = ""; + } + + // If an exeption was provided, convert that exception to a usable string and attach + // it to the end of the msg method. + if (tr != null) { + msg += "\n" + Log.getStackTraceString(tr); + } + + // This is functionally identical to Log.x(tag, useMsg); + // For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg) + Log.println(priority, tag, useMsg); + + // If this isn't the last node in the chain, move things along. + if (mNext != null) { + mNext.println(priority, tag, msg, tr); + } + } +} diff --git a/common/src/java/com/example/android/common/logger/MessageOnlyLogFilter.java b/common/src/java/com/example/android/common/logger/MessageOnlyLogFilter.java new file mode 100644 index 00000000..19967dcd --- /dev/null +++ b/common/src/java/com/example/android/common/logger/MessageOnlyLogFilter.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.common.logger; + +/** + * Simple {@link LogNode} filter, removes everything except the message. + * Useful for situations like on-screen log output where you don't want a lot of metadata displayed, + * just easy-to-read message updates as they're happening. + */ +public class MessageOnlyLogFilter implements LogNode { + + LogNode mNext; + + /** + * Takes the "next" LogNode as a parameter, to simplify chaining. + * + * @param next The next LogNode in the pipeline. + */ + public MessageOnlyLogFilter(LogNode next) { + mNext = next; + } + + public MessageOnlyLogFilter() { + } + + @Override + public void println(int priority, String tag, String msg, Throwable tr) { + if (mNext != null) { + getNext().println(Log.NONE, null, msg, null); + } + } + + /** + * Returns the next LogNode in the chain. + */ + public LogNode getNext() { + return mNext; + } + + /** + * Sets the LogNode data will be sent to.. + */ + public void setNext(LogNode node) { + mNext = node; + } + +} diff --git a/common/src/com/example/android/common/media/CameraHelper.java b/common/src/java/com/example/android/common/media/CameraHelper.java index 1fa84167..1fa84167 100644 --- a/common/src/com/example/android/common/media/CameraHelper.java +++ b/common/src/java/com/example/android/common/media/CameraHelper.java diff --git a/common/src/java/com/example/android/common/media/MediaCodecWrapper.java b/common/src/java/com/example/android/common/media/MediaCodecWrapper.java new file mode 100644 index 00000000..a511221f --- /dev/null +++ b/common/src/java/com/example/android/common/media/MediaCodecWrapper.java @@ -0,0 +1,386 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.common.media; + +import android.media.*; +import android.os.Handler; +import android.os.Looper; +import android.view.Surface; + +import java.nio.ByteBuffer; +import java.util.ArrayDeque; +import java.util.Queue; + +/** + * Simplifies the MediaCodec interface by wrapping around the buffer processing operations. + */ +public class MediaCodecWrapper { + + // Handler to use for {@code OutputSampleListener} and {code OutputFormatChangedListener} + // callbacks + private Handler mHandler; + + + // Callback when media output format changes. + public interface OutputFormatChangedListener { + void outputFormatChanged(MediaCodecWrapper sender, MediaFormat newFormat); + } + + private OutputFormatChangedListener mOutputFormatChangedListener = null; + + /** + * Callback for decodes frames. Observers can register a listener for optional stream + * of decoded data + */ + public interface OutputSampleListener { + void outputSample(MediaCodecWrapper sender, MediaCodec.BufferInfo info, ByteBuffer buffer); + } + + /** + * The {@link MediaCodec} that is managed by this class. + */ + private MediaCodec mDecoder; + + // References to the internal buffers managed by the codec. The codec + // refers to these buffers by index, never by reference so it's up to us + // to keep track of which buffer is which. + private ByteBuffer[] mInputBuffers; + private ByteBuffer[] mOutputBuffers; + + // Indices of the input buffers that are currently available for writing. We'll + // consume these in the order they were dequeued from the codec. + private Queue<Integer> mAvailableInputBuffers; + + // Indices of the output buffers that currently hold valid data, in the order + // they were produced by the codec. + private Queue<Integer> mAvailableOutputBuffers; + + // Information about each output buffer, by index. Each entry in this array + // is valid if and only if its index is currently contained in mAvailableOutputBuffers. + private MediaCodec.BufferInfo[] mOutputBufferInfo; + + // An (optional) stream that will receive decoded data. + private OutputSampleListener mOutputSampleListener; + + private MediaCodecWrapper(MediaCodec codec) { + mDecoder = codec; + codec.start(); + mInputBuffers = codec.getInputBuffers(); + mOutputBuffers = codec.getOutputBuffers(); + mOutputBufferInfo = new MediaCodec.BufferInfo[mOutputBuffers.length]; + mAvailableInputBuffers = new ArrayDeque<Integer>(mOutputBuffers.length); + mAvailableOutputBuffers = new ArrayDeque<Integer>(mInputBuffers.length); + } + + /** + * Releases resources and ends the encoding/decoding session. + */ + public void stopAndRelease() { + mDecoder.stop(); + mDecoder.release(); + mDecoder = null; + mHandler = null; + } + + /** + * Getter for the registered {@link OutputFormatChangedListener} + */ + public OutputFormatChangedListener getOutputFormatChangedListener() { + return mOutputFormatChangedListener; + } + + /** + * + * @param outputFormatChangedListener the listener for callback. + * @param handler message handler for posting the callback. + */ + public void setOutputFormatChangedListener(final OutputFormatChangedListener + outputFormatChangedListener, Handler handler) { + mOutputFormatChangedListener = outputFormatChangedListener; + + // Making sure we don't block ourselves due to a bad implementation of the callback by + // using a handler provided by client. + Looper looper; + mHandler = handler; + if (outputFormatChangedListener != null && mHandler == null) { + if ((looper = Looper.myLooper()) != null) { + mHandler = new Handler(); + } else { + throw new IllegalArgumentException( + "Looper doesn't exist in the calling thread"); + } + } + } + + /** + * Constructs the {@link MediaCodecWrapper} wrapper object around the video codec. + * The codec is created using the encapsulated information in the + * {@link MediaFormat} object. + * + * @param trackFormat The format of the media object to be decoded. + * @param surface Surface to render the decoded frames. + * @return + */ + public static MediaCodecWrapper fromVideoFormat(final MediaFormat trackFormat, + Surface surface) { + MediaCodecWrapper result = null; + MediaCodec videoCodec = null; + + // BEGIN_INCLUDE(create_codec) + final String mimeType = trackFormat.getString(MediaFormat.KEY_MIME); + + // Check to see if this is actually a video mime type. If it is, then create + // a codec that can decode this mime type. + if (mimeType.contains("video/")) { + videoCodec = MediaCodec.createDecoderByType(mimeType); + videoCodec.configure(trackFormat, surface, null, 0); + + } + + // If codec creation was successful, then create a wrapper object around the + // newly created codec. + if (videoCodec != null) { + result = new MediaCodecWrapper(videoCodec); + } + // END_INCLUDE(create_codec) + + return result; + } + + + /** + * Write a media sample to the decoder. + * + * A "sample" here refers to a single atomic access unit in the media stream. The definition + * of "access unit" is dependent on the type of encoding used, but it typically refers to + * a single frame of video or a few seconds of audio. {@link android.media.MediaExtractor} + * extracts data from a stream one sample at a time. + * + * @param input A ByteBuffer containing the input data for one sample. The buffer must be set + * up for reading, with its position set to the beginning of the sample data and its limit + * set to the end of the sample data. + * + * @param presentationTimeUs The time, relative to the beginning of the media stream, + * at which this buffer should be rendered. + * + * @param flags Flags to pass to the decoder. See {@link MediaCodec#queueInputBuffer(int, + * int, int, long, int)} + * + * @throws MediaCodec.CryptoException + */ + public boolean writeSample(final ByteBuffer input, + final MediaCodec.CryptoInfo crypto, + final long presentationTimeUs, + final int flags) throws MediaCodec.CryptoException, WriteException { + boolean result = false; + int size = input.remaining(); + + // check if we have dequed input buffers available from the codec + if (size > 0 && !mAvailableInputBuffers.isEmpty()) { + int index = mAvailableInputBuffers.remove(); + ByteBuffer buffer = mInputBuffers[index]; + + // we can't write our sample to a lesser capacity input buffer. + if (size > buffer.capacity()) { + throw new MediaCodecWrapper.WriteException(String.format( + "Insufficient capacity in MediaCodec buffer: " + + "tried to write %d, buffer capacity is %d.", + input.remaining(), + buffer.capacity())); + } + + buffer.clear(); + buffer.put(input); + + // Submit the buffer to the codec for decoding. The presentationTimeUs + // indicates the position (play time) for the current sample. + if (crypto == null) { + mDecoder.queueInputBuffer(index, 0, size, presentationTimeUs, flags); + } else { + mDecoder.queueSecureInputBuffer(index, 0, crypto, presentationTimeUs, flags); + } + result = true; + } + return result; + } + + static MediaCodec.CryptoInfo cryptoInfo= new MediaCodec.CryptoInfo(); + + /** + * Write a media sample to the decoder. + * + * A "sample" here refers to a single atomic access unit in the media stream. The definition + * of "access unit" is dependent on the type of encoding used, but it typically refers to + * a single frame of video or a few seconds of audio. {@link android.media.MediaExtractor} + * extracts data from a stream one sample at a time. + * + * @param extractor Instance of {@link android.media.MediaExtractor} wrapping the media. + * + * @param presentationTimeUs The time, relative to the beginning of the media stream, + * at which this buffer should be rendered. + * + * @param flags Flags to pass to the decoder. See {@link MediaCodec#queueInputBuffer(int, + * int, int, long, int)} + * + * @throws MediaCodec.CryptoException + */ + public boolean writeSample(final MediaExtractor extractor, + final boolean isSecure, + final long presentationTimeUs, + int flags) { + boolean result = false; + boolean isEos = false; + + if (!mAvailableInputBuffers.isEmpty()) { + int index = mAvailableInputBuffers.remove(); + ByteBuffer buffer = mInputBuffers[index]; + + // reads the sample from the file using extractor into the buffer + int size = extractor.readSampleData(buffer, 0); + if (size <= 0) { + flags |= MediaCodec.BUFFER_FLAG_END_OF_STREAM; + } + + // Submit the buffer to the codec for decoding. The presentationTimeUs + // indicates the position (play time) for the current sample. + if (!isSecure) { + mDecoder.queueInputBuffer(index, 0, size, presentationTimeUs, flags); + } else { + extractor.getSampleCryptoInfo(cryptoInfo); + mDecoder.queueSecureInputBuffer(index, 0, cryptoInfo, presentationTimeUs, flags); + } + + result = true; + } + return result; + } + + /** + * Performs a peek() operation in the queue to extract media info for the buffer ready to be + * released i.e. the head element of the queue. + * + * @param out_bufferInfo An output var to hold the buffer info. + * + * @return True, if the peek was successful. + */ + public boolean peekSample(MediaCodec.BufferInfo out_bufferInfo) { + // dequeue available buffers and synchronize our data structures with the codec. + update(); + boolean result = false; + if (!mAvailableOutputBuffers.isEmpty()) { + int index = mAvailableOutputBuffers.peek(); + MediaCodec.BufferInfo info = mOutputBufferInfo[index]; + // metadata of the sample + out_bufferInfo.set( + info.offset, + info.size, + info.presentationTimeUs, + info.flags); + result = true; + } + return result; + } + + /** + * Processes, releases and optionally renders the output buffer available at the head of the + * queue. All observers are notified with a callback. See {@link + * OutputSampleListener#outputSample(MediaCodecWrapper, android.media.MediaCodec.BufferInfo, + * java.nio.ByteBuffer)} + * + * @param render True, if the buffer is to be rendered on the {@link Surface} configured + * + */ + public void popSample(boolean render) { + // dequeue available buffers and synchronize our data structures with the codec. + update(); + if (!mAvailableOutputBuffers.isEmpty()) { + int index = mAvailableOutputBuffers.remove(); + + if (render && mOutputSampleListener != null) { + ByteBuffer buffer = mOutputBuffers[index]; + MediaCodec.BufferInfo info = mOutputBufferInfo[index]; + mOutputSampleListener.outputSample(this, info, buffer); + } + + // releases the buffer back to the codec + mDecoder.releaseOutputBuffer(index, render); + } + } + + /** + * Synchronize this object's state with the internal state of the wrapped + * MediaCodec. + */ + private void update() { + // BEGIN_INCLUDE(update_codec_state) + int index; + + // Get valid input buffers from the codec to fill later in the same order they were + // made available by the codec. + while ((index = mDecoder.dequeueInputBuffer(0)) != MediaCodec.INFO_TRY_AGAIN_LATER) { + mAvailableInputBuffers.add(index); + } + + + // Likewise with output buffers. If the output buffers have changed, start using the + // new set of output buffers. If the output format has changed, notify listeners. + MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); + while ((index = mDecoder.dequeueOutputBuffer(info, 0)) != MediaCodec.INFO_TRY_AGAIN_LATER) { + switch (index) { + case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED: + mOutputBuffers = mDecoder.getOutputBuffers(); + mOutputBufferInfo = new MediaCodec.BufferInfo[mOutputBuffers.length]; + mAvailableOutputBuffers.clear(); + break; + case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED: + if (mOutputFormatChangedListener != null) { + mHandler.post(new Runnable() { + @Override + public void run() { + mOutputFormatChangedListener + .outputFormatChanged(MediaCodecWrapper.this, + mDecoder.getOutputFormat()); + + } + }); + } + break; + default: + // Making sure the index is valid before adding to output buffers. We've already + // handled INFO_TRY_AGAIN_LATER, INFO_OUTPUT_FORMAT_CHANGED & + // INFO_OUTPUT_BUFFERS_CHANGED i.e all the other possible return codes but + // asserting index value anyways for future-proofing the code. + if(index >= 0) { + mOutputBufferInfo[index] = info; + mAvailableOutputBuffers.add(index); + } else { + throw new IllegalStateException("Unknown status from dequeueOutputBuffer"); + } + break; + } + + } + // END_INCLUDE(update_codec_state) + + } + + private class WriteException extends Throwable { + private WriteException(final String detailMessage) { + super(detailMessage); + } + } +} diff --git a/common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java b/common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java new file mode 100644 index 00000000..0a80d880 --- /dev/null +++ b/common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java @@ -0,0 +1,75 @@ +/** + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.example.android.common.play; + +import android.app.Dialog; +import android.content.IntentSender; +import android.support.v4.app.FragmentActivity; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesClient; + +/** + * Helper to handle errors from Google Play Services connections. + * + */ +public class GoogleServicesConnectionFailedHelper implements + GooglePlayServicesClient.OnConnectionFailedListener { + + FragmentActivity mActivity; + int mRequestCode = -1; + + public GoogleServicesConnectionFailedHelper(FragmentActivity mActivity, int requestCode) { + this.mActivity = mActivity; + mRequestCode = requestCode; + } + + @Override + public void onConnectionFailed(ConnectionResult connectionResult) { + + /* + * Google Play services can resolve some errors it detects. + * If the error has a resolution, try sending an Intent to + * start a Google Play services activity that can resolve + * error. + */ + if (connectionResult.hasResolution()) { + try { + // Start an Activity that tries to resolve the error + connectionResult.startResolutionForResult(mActivity, mRequestCode); + /* + * Thrown if Google Play services canceled the original + * PendingIntent + */ + } catch (IntentSender.SendIntentException e) { + // Log the error + e.printStackTrace(); + } + } else { + /* + * If no resolution is available, display a dialog to the + * user with the error. + */ + PlayHelper.ErrorDialogFragment fragment = new PlayHelper.ErrorDialogFragment(); + fragment.setDialog(new Dialog(mActivity)); + fragment.show(mActivity.getSupportFragmentManager(), null); + + } + } +} + diff --git a/common/src/com/example/android/common/play/PlayHelper.java b/common/src/java/com/example/android/common/play/PlayHelper.java index c38c2bb6..95b8554c 100644 --- a/common/src/com/example/android/common/play/PlayHelper.java +++ b/common/src/java/com/example/android/common/play/PlayHelper.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + package com.example.android.common.play; import android.app.Activity; @@ -99,4 +116,5 @@ public class PlayHelper { return mDialog; } } + } diff --git a/common/src/com/example/android/common/Pools.java b/common/src/java/com/example/android/common/util/Pools.java index b31749a1..1b7edb02 100644 --- a/common/src/com/example/android/common/Pools.java +++ b/common/src/java/com/example/android/common/util/Pools.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.android.common; +package com.example.android.common.util; /** * Helper class for creating pools of objects. Creating new objects is an |