Add prebuilt browseable samples as static files.
Change-Id: Ifb5382223343400882834d2dd9c182c3df602e34
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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.accounts;
|
||||
|
||||
import android.accounts.AbstractAccountAuthenticator;
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountAuthenticatorResponse;
|
||||
import android.accounts.NetworkErrorException;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
public class GenericAccountService extends Service {
|
||||
private static final String TAG = "GenericAccountService";
|
||||
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(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.
|
||||
//
|
||||
// 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, accountType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.i(TAG, "Service created");
|
||||
mAuthenticator = new Authenticator(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.i(TAG, "Service destroyed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return mAuthenticator.getIBinder();
|
||||
}
|
||||
|
||||
public class Authenticator extends AbstractAccountAuthenticator {
|
||||
public Authenticator(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse,
|
||||
String s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle addAccount(AccountAuthenticatorResponse accountAuthenticatorResponse,
|
||||
String s, String s2, String[] strings, Bundle bundle)
|
||||
throws NetworkErrorException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse,
|
||||
Account account, Bundle bundle)
|
||||
throws NetworkErrorException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse,
|
||||
Account account, String s, Bundle bundle)
|
||||
throws NetworkErrorException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthTokenLabel(String s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse,
|
||||
Account account, String s, Bundle bundle)
|
||||
throws NetworkErrorException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bundle hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse,
|
||||
Account account, String[] strings)
|
||||
throws NetworkErrorException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,356 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modifications:
|
||||
* -Imported from AOSP frameworks/base/core/java/com/android/internal/content
|
||||
* -Changed package name
|
||||
*/
|
||||
|
||||
package com.example.android.common.db;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helper for building selection clauses for {@link SQLiteDatabase}.
|
||||
*
|
||||
* <p>This class provides a convenient frontend for working with SQL. Instead of composing statements
|
||||
* manually using string concatenation, method calls are used to construct the statement one
|
||||
* clause at a time. These methods can be chained together.
|
||||
*
|
||||
* <p>If multiple where() statements are provided, they're combined using {@code AND}.
|
||||
*
|
||||
* <p>Example:
|
||||
*
|
||||
* <pre>
|
||||
* SelectionBuilder builder = new SelectionBuilder();
|
||||
* Cursor c = builder.table(FeedContract.Entry.TABLE_NAME) // String TABLE_NAME = "entry"
|
||||
* .where(FeedContract.Entry._ID + "=?", id); // String _ID = "_ID"
|
||||
* .query(db, projection, sortOrder)
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
* <p>In this example, the table name and filters ({@code WHERE} clauses) are both explicitly
|
||||
* specified via method call. SelectionBuilder takes care of issuing a "query" command to the
|
||||
* database, and returns the resulting {@link Cursor} object.
|
||||
*
|
||||
* <p>Inner {@code JOIN}s can be accomplished using the mapToTable() function. The map() function
|
||||
* can be used to create new columns based on arbitrary (SQL-based) criteria. In advanced usage,
|
||||
* entire subqueries can be passed into the map() function.
|
||||
*
|
||||
* <p>Advanced example:
|
||||
*
|
||||
* <pre>
|
||||
* // String SESSIONS_JOIN_BLOCKS_ROOMS = "sessions "
|
||||
* // + "LEFT OUTER JOIN blocks ON sessions.block_id=blocks.block_id "
|
||||
* // + "LEFT OUTER JOIN rooms ON sessions.room_id=rooms.room_id";
|
||||
*
|
||||
* // String Subquery.BLOCK_NUM_STARRED_SESSIONS =
|
||||
* // "(SELECT COUNT(1) FROM "
|
||||
* // + Tables.SESSIONS + " WHERE " + Qualified.SESSIONS_BLOCK_ID + "="
|
||||
* // + Qualified.BLOCKS_BLOCK_ID + " AND " + Qualified.SESSIONS_STARRED + "=1)";
|
||||
*
|
||||
* String Subqery.BLOCK_SESSIONS_COUNT =
|
||||
* Cursor c = builder.table(Tables.SESSIONS_JOIN_BLOCKS_ROOMS)
|
||||
* .map(Blocks.NUM_STARRED_SESSIONS, Subquery.BLOCK_NUM_STARRED_SESSIONS)
|
||||
* .mapToTable(Sessions._ID, Tables.SESSIONS)
|
||||
* .mapToTable(Sessions.SESSION_ID, Tables.SESSIONS)
|
||||
* .mapToTable(Sessions.BLOCK_ID, Tables.SESSIONS)
|
||||
* .mapToTable(Sessions.ROOM_ID, Tables.SESSIONS)
|
||||
* .where(Qualified.SESSIONS_BLOCK_ID + "=?", blockId);
|
||||
* </pre>
|
||||
*
|
||||
* <p>In this example, we have two different types of {@code JOIN}s: a left outer join using a
|
||||
* modified table name (since this class doesn't directly support these), and an inner join using
|
||||
* the mapToTable() function. The map() function is used to insert a count based on specific
|
||||
* criteria, executed as a sub-query.
|
||||
*
|
||||
* This class is <em>not</em> thread safe.
|
||||
*/
|
||||
public class SelectionBuilder {
|
||||
private static final String TAG = "basicsyncadapter";
|
||||
|
||||
private String mTable = null;
|
||||
private Map<String, String> mProjectionMap = new HashMap<String, String>();
|
||||
private StringBuilder mSelection = new StringBuilder();
|
||||
private ArrayList<String> mSelectionArgs = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Reset any internal state, allowing this builder to be recycled.
|
||||
*
|
||||
* <p>Calling this method is more efficient than creating a new SelectionBuilder object.
|
||||
*
|
||||
* @return Fluent interface
|
||||
*/
|
||||
public SelectionBuilder reset() {
|
||||
mTable = null;
|
||||
mSelection.setLength(0);
|
||||
mSelectionArgs.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append the given selection clause to the internal state. Each clause is
|
||||
* surrounded with parenthesis and combined using {@code AND}.
|
||||
*
|
||||
* <p>In the most basic usage, simply provide a selection in SQL {@code WHERE} statement format.
|
||||
*
|
||||
* <p>Example:
|
||||
*
|
||||
* <pre>
|
||||
* .where("blog_posts.category = 'PROGRAMMING');
|
||||
* </pre>
|
||||
*
|
||||
* <p>User input should never be directly supplied as as part of the selection statement.
|
||||
* Instead, use positional parameters in your selection statement, then pass the user input
|
||||
* in via the selectionArgs parameter. This prevents SQL escape characters in user input from
|
||||
* causing unwanted side effects. (Failure to follow this convention may have security
|
||||
* implications.)
|
||||
*
|
||||
* <p>Positional parameters are specified using the '?' character.
|
||||
*
|
||||
* <p>Example:
|
||||
* <pre>
|
||||
* .where("blog_posts.title contains ?, userSearchString);
|
||||
* </pre>
|
||||
*
|
||||
* @param selection SQL where statement
|
||||
* @param selectionArgs Values to substitute for positional parameters ('?' characters in
|
||||
* {@code selection} statement. Will be automatically escaped.
|
||||
* @return Fluent interface
|
||||
*/
|
||||
public SelectionBuilder where(String selection, String... selectionArgs) {
|
||||
if (TextUtils.isEmpty(selection)) {
|
||||
if (selectionArgs != null && selectionArgs.length > 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Valid selection required when including arguments=");
|
||||
}
|
||||
|
||||
// Shortcut when clause is empty
|
||||
return this;
|
||||
}
|
||||
|
||||
if (mSelection.length() > 0) {
|
||||
mSelection.append(" AND ");
|
||||
}
|
||||
|
||||
mSelection.append("(").append(selection).append(")");
|
||||
if (selectionArgs != null) {
|
||||
Collections.addAll(mSelectionArgs, selectionArgs);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Table name to use for SQL {@code FROM} statement.
|
||||
*
|
||||
* <p>This method may only be called once. If multiple tables are required, concatenate them
|
||||
* in SQL-format (typically comma-separated).
|
||||
*
|
||||
* <p>If you need to do advanced {@code JOIN}s, they can also be specified here.
|
||||
*
|
||||
* See also: mapToTable()
|
||||
*
|
||||
* @param table Table name
|
||||
* @return Fluent interface
|
||||
*/
|
||||
public SelectionBuilder table(String table) {
|
||||
mTable = table;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a table name has been supplied using table().
|
||||
*
|
||||
* @throws IllegalStateException if table not set
|
||||
*/
|
||||
private void assertTable() {
|
||||
if (mTable == null) {
|
||||
throw new IllegalStateException("Table not specified");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an inner join.
|
||||
*
|
||||
* <p>Map columns from a secondary table onto the current result set. References to the column
|
||||
* specified in {@code column} will be replaced with {@code table.column} in the SQL {@code
|
||||
* SELECT} clause.
|
||||
*
|
||||
* @param column Column name to join on. Must be the same in both tables.
|
||||
* @param table Secondary table to join.
|
||||
* @return Fluent interface
|
||||
*/
|
||||
public SelectionBuilder mapToTable(String column, String table) {
|
||||
mProjectionMap.put(column, table + "." + column);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new column based on custom criteria (such as aggregate functions).
|
||||
*
|
||||
* <p>This adds a new column to the result set, based upon custom criteria in SQL format. This
|
||||
* is equivalent to the SQL statement: {@code SELECT toClause AS fromColumn}
|
||||
*
|
||||
* <p>This method is useful for executing SQL sub-queries.
|
||||
*
|
||||
* @param fromColumn Name of column for mapping
|
||||
* @param toClause SQL string representing data to be mapped
|
||||
* @return Fluent interface
|
||||
*/
|
||||
public SelectionBuilder map(String fromColumn, String toClause) {
|
||||
mProjectionMap.put(fromColumn, toClause + " AS " + fromColumn);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return selection string based on current internal state.
|
||||
*
|
||||
* @return Current selection as a SQL statement
|
||||
* @see #getSelectionArgs()
|
||||
*/
|
||||
public String getSelection() {
|
||||
return mSelection.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return selection arguments based on current internal state.
|
||||
*
|
||||
* @see #getSelection()
|
||||
*/
|
||||
public String[] getSelectionArgs() {
|
||||
return mSelectionArgs.toArray(new String[mSelectionArgs.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process user-supplied projection (column list).
|
||||
*
|
||||
* <p>In cases where a column is mapped to another data source (either another table, or an
|
||||
* SQL sub-query), the column name will be replaced with a more specific, SQL-compatible
|
||||
* representation.
|
||||
*
|
||||
* Assumes that incoming columns are non-null.
|
||||
*
|
||||
* <p>See also: map(), mapToTable()
|
||||
*
|
||||
* @param columns User supplied projection (column list).
|
||||
*/
|
||||
private void mapColumns(String[] columns) {
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
final String target = mProjectionMap.get(columns[i]);
|
||||
if (target != null) {
|
||||
columns[i] = target;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a description of this builder's state. Does NOT output SQL.
|
||||
*
|
||||
* @return Human-readable internal state
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SelectionBuilder[table=" + mTable + ", selection=" + getSelection()
|
||||
+ ", selectionArgs=" + Arrays.toString(getSelectionArgs()) + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute query (SQL {@code SELECT}) against specified database.
|
||||
*
|
||||
* <p>Using a null projection (column list) is not supported.
|
||||
*
|
||||
* @param db Database to query.
|
||||
* @param columns Database projection (column list) to return, must be non-NULL.
|
||||
* @param orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the
|
||||
* ORDER BY itself). Passing null will use the default sort order, which may be
|
||||
* unordered.
|
||||
* @return A {@link Cursor} object, which is positioned before the first entry. Note that
|
||||
* {@link Cursor}s are not synchronized, see the documentation for more details.
|
||||
*/
|
||||
public Cursor query(SQLiteDatabase db, String[] columns, String orderBy) {
|
||||
return query(db, columns, null, null, orderBy, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute query ({@code SELECT}) against database.
|
||||
*
|
||||
* <p>Using a null projection (column list) is not supported.
|
||||
*
|
||||
* @param db Database to query.
|
||||
* @param columns Database projection (column list) to return, must be non-null.
|
||||
* @param groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause
|
||||
* (excluding the GROUP BY itself). Passing null will cause the rows to not be
|
||||
* grouped.
|
||||
* @param having A filter declare which row groups to include in the cursor, if row grouping is
|
||||
* being used, formatted as an SQL HAVING clause (excluding the HAVING itself).
|
||||
* Passing null will cause all row groups to be included, and is required when
|
||||
* row grouping is not being used.
|
||||
* @param orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the
|
||||
* ORDER BY itself). Passing null will use the default sort order, which may be
|
||||
* unordered.
|
||||
* @param limit Limits the number of rows returned by the query, formatted as LIMIT clause.
|
||||
* Passing null denotes no LIMIT clause.
|
||||
* @return A {@link Cursor} object, which is positioned before the first entry. Note that
|
||||
* {@link Cursor}s are not synchronized, see the documentation for more details.
|
||||
*/
|
||||
public Cursor query(SQLiteDatabase db, String[] columns, String groupBy,
|
||||
String having, String orderBy, String limit) {
|
||||
assertTable();
|
||||
if (columns != null) mapColumns(columns);
|
||||
Log.v(TAG, "query(columns=" + Arrays.toString(columns) + ") " + this);
|
||||
return db.query(mTable, columns, getSelection(), getSelectionArgs(), groupBy, having,
|
||||
orderBy, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an {@code UPDATE} against database.
|
||||
*
|
||||
* @param db Database to query.
|
||||
* @param values A map from column names to new column values. null is a valid value that will
|
||||
* be translated to NULL
|
||||
* @return The number of rows affected.
|
||||
*/
|
||||
public int update(SQLiteDatabase db, ContentValues values) {
|
||||
assertTable();
|
||||
Log.v(TAG, "update() " + this);
|
||||
return db.update(mTable, values, getSelection(), getSelectionArgs());
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute {@code DELETE} against database.
|
||||
*
|
||||
* @param db Database to query.
|
||||
* @return The number of rows affected.
|
||||
*/
|
||||
public int delete(SQLiteDatabase db) {
|
||||
assertTable();
|
||||
Log.v(TAG, "delete() " + this);
|
||||
return db.delete(mTable, getSelection(), getSelectionArgs());
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user