Commit 3f703ecf authored by Thomas Krex's avatar Thomas Krex
Browse files

added drawertogglelibrary to git repository

parent dfd30545
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DrawerToggleLibrary</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.source=1.6
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.actionbarsherlock.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application />
</manifest>
<?xml version="1.0" encoding="UTF-8"?>
<lint>
</lint>
\ No newline at end of file
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-19
android.library=true
android.library.reference.1=../actionbarsherlock
<resources>
<string name="app_name">ActionBarSherlockDrawerToggle</string>
</resources>
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
package com.actionbarsherlock.app;
/* 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.
*/
import java.lang.reflect.Method;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* This class encapsulates some awful hacks.
*
* Before JB-MR2 (API 18) it was not possible to change the home-as-up indicator
* glyph in an action bar without some really gross hacks. Since the MR2 SDK is
* not published as of this writing, the new API is accessed via reflection here
* if available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class ActionBarDrawerToggleSherlock {
private static final String TAG = "ActionBarDrawerToggleSherlock";
private static final int[] THEME_ATTRS = new int[] { R.attr.homeAsUpIndicator };
public static Object setActionBarUpIndicator(Object info,
Activity activity, Drawable drawable, int contentDescRes) {
SherlockFragmentActivity sherlockActivity = (SherlockFragmentActivity) activity;
if (info == null) {
info = new SetIndicatorInfo(activity);
}
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.setHomeAsUpIndicator != null) {
try {
final ActionBar actionBar = sherlockActivity
.getSupportActionBar();
sii.setHomeAsUpIndicator.invoke(actionBar, drawable);
sii.setHomeActionContentDescription.invoke(actionBar,
contentDescRes);
} catch (Exception e) {
Log.w(TAG, "Couldn't set home-as-up indicator via JB-MR2 API",
e);
}
} else if (sii.upIndicatorView != null) {
sii.upIndicatorView.setImageDrawable(drawable);
} else {
Log.w(TAG, "Couldn't set home-as-up indicator");
}
return info;
}
public static Object setActionBarDescription(Object info,
Activity activity, int contentDescRes) {
SherlockFragmentActivity sherlockActivity = (SherlockFragmentActivity) activity;
if (info == null) {
info = new SetIndicatorInfo(activity);
}
final SetIndicatorInfo sii = (SetIndicatorInfo) info;
if (sii.setHomeAsUpIndicator != null) {
try {
final ActionBar actionBar = sherlockActivity
.getSupportActionBar();
sii.setHomeActionContentDescription.invoke(actionBar,
contentDescRes);
} catch (Exception e) {
Log.w(TAG, "Couldn't set content description via JB-MR2 API", e);
}
}
return info;
}
public static Drawable getThemeUpIndicator(Activity activity) {
final TypedArray a = activity.obtainStyledAttributes(THEME_ATTRS);
final Drawable result = a.getDrawable(0);
a.recycle();
return result;
}
private static class SetIndicatorInfo {
public Method setHomeAsUpIndicator;
public Method setHomeActionContentDescription;
public ImageView upIndicatorView;
SetIndicatorInfo(Activity activity) {
try {
setHomeAsUpIndicator = ActionBar.class.getDeclaredMethod(
"setHomeAsUpIndicator", Drawable.class);
setHomeActionContentDescription = ActionBar.class
.getDeclaredMethod("setHomeActionContentDescription",
Integer.TYPE);
// If we got the method we won't need the stuff below.
return;
} catch (NoSuchMethodException e) {
// Oh well. We'll use the other mechanism below instead.
}
final View home = activity.findViewById(android.R.id.home);
if (home == null) {
// Action bar doesn't have a known configuration, an OEM messed
// with things.
// Try ActionBarSherlock up
ViewGroup sherlockRoot = (ViewGroup)activity.findViewById(R.id.abs__action_bar_container);
if(sherlockRoot != null){
walkViewGroupForSherlockUpView(sherlockRoot);
}
return;
}
final ViewGroup parent = (ViewGroup) home.getParent();
final int childCount = parent.getChildCount();
if (childCount != 2) {
// No idea which one will be the right one, an OEM messed with
// things.
return;
}
final View first = parent.getChildAt(0);
final View second = parent.getChildAt(1);
final View up = first.getId() == android.R.id.home ? second : first;
if (up instanceof ImageView) {
// Jackpot! (Probably...)
upIndicatorView = (ImageView) up;
}
}
/**
* Method to walk the View hierarchy and look for a visible
* ActionBarSherlock UP View. The problem is, that the View hierarchy of
* ActionBarSherlock 4.3.1 contains multiple Views with the id
* R.id.abs__up, so we look for the one that is actually visible to the
* user. This is a recursive method, ideally the root View
* ActionBarSherlock should be passed in as the parameter.
*/
private void walkViewGroupForSherlockUpView(ViewGroup group) {
for (int i = 0; i < group.getChildCount(); i++) {
View current = group.getChildAt(i);
if (current.getId() == R.id.abs__up
&& current.getVisibility() == View.VISIBLE) {
upIndicatorView = (ImageView) current;
}
if (current instanceof ViewGroup) {
walkViewGroupForSherlockUpView((ViewGroup) current);
}
}
}
}
}
\ No newline at end of file
package com.actionbarsherlock.app;
/*
* 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.
*/
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import com.actionbarsherlock.view.MenuItem;
/**
* This class provides a handy way to tie together the functionality of
* {@link DrawerLayout} and the framework <code>ActionBar</code> to implement
* the recommended design for navigation drawers.
*
* <p>
* To use <code>ActionBarDrawerToggle</code>, create one in your Activity and
* call through to the following methods corresponding to your Activity
* callbacks:
* </p>
*
* <ul>
* <li>
* {@link Activity#onConfigurationChanged(android.content.res.Configuration)
* onConfigurationChanged}</li>
* <li>{@link Activity#onOptionsItemSelected(android.view.MenuItem)
* onOptionsItemSelected}</li>
* </ul>
*
* <p>
* Call {@link #syncState()} from your <code>Activity</code>'s
* {@link Activity#onPostCreate(android.os.Bundle) onPostCreate} to synchronize
* the indicator with the state of the linked DrawerLayout after
* <code>onRestoreInstanceState</code> has occurred.
* </p>
*
* <p>
* <code>ActionBarDrawerToggle</code> can be used directly as a
* {@link DrawerLayout.DrawerListener}, or if you are already providing your own
* listener, call through to each of the listener methods from your own.
* </p>
*/
public class ActionBarSherlockDrawerToggle implements
DrawerLayout.DrawerListener {
private interface ActionBarDrawerToggleImpl {
Drawable getThemeUpIndicator(Activity activity);
Object setActionBarUpIndicator(Object info, Activity activity,
Drawable themeImage, int contentDescRes);
Object setActionBarDescription(Object info, Activity activity,
int contentDescRes);
}
private static class ActionBarDrawerToggleImplSherlock implements
ActionBarDrawerToggleImpl {
@Override
public Drawable getThemeUpIndicator(Activity activity) {
return ActionBarDrawerToggleSherlock.getThemeUpIndicator(activity);
}
@Override
public Object setActionBarUpIndicator(Object info, Activity activity,
Drawable themeImage, int contentDescRes) {
return ActionBarDrawerToggleSherlock.setActionBarUpIndicator(info,
activity, themeImage, contentDescRes);
}
@Override
public Object setActionBarDescription(Object info, Activity activity,
int contentDescRes) {
return ActionBarDrawerToggleSherlock.setActionBarDescription(info,
activity, contentDescRes);
}
}
private static final ActionBarDrawerToggleImpl IMPL;
static {
IMPL = new ActionBarDrawerToggleImplSherlock();
}
// android.R.id.home as defined by public API in v11
private static final int ID_HOME = 0x0102002c;
private final Activity mActivity;
private final DrawerLayout mDrawerLayout;
private boolean mDrawerIndicatorEnabled = true;
private Drawable mThemeImage;
private Drawable mDrawerImage;
private final SlideDrawable mSlider;
private final int mDrawerImageResource;
private final int mOpenDrawerContentDescRes;
private final int mCloseDrawerContentDescRes;
private Object mSetIndicatorInfo;
/**
* Construct a new ActionBarDrawerToggle.
*
* <p>
* The given {@link Activity} will be linked to the specified
* {@link DrawerLayout}. The provided drawer indicator drawable will animate
* slightly off-screen as the drawer is opened, indicating that in the open
* state the drawer will move off-screen when pressed and in the closed
* state the drawer will move on-screen when pressed.
* </p>
*
* <p>
* String resources must be provided to describe the open/close drawer
* actions for accessibility services.
* </p>
*
* @param activity
* The Activity hosting the drawer
* @param drawerLayout
* The DrawerLayout to link to the given Activity's ActionBar
* @param drawerImageRes
* A Drawable resource to use as the drawer indicator
* @param openDrawerContentDescRes
* A String resource to describe the "open drawer" action for
* accessibility
* @param closeDrawerContentDescRes
* A String resource to describe the "close drawer" action for
* accessibility
*/
public ActionBarSherlockDrawerToggle(Activity activity,
DrawerLayout drawerLayout, int drawerImageRes,
int openDrawerContentDescRes, int closeDrawerContentDescRes) {
mActivity = activity;
mDrawerLayout = drawerLayout;
mDrawerImageResource = drawerImageRes;
mOpenDrawerContentDescRes = openDrawerContentDescRes;
mCloseDrawerContentDescRes = closeDrawerContentDescRes;
mThemeImage = IMPL.getThemeUpIndicator(activity);
mDrawerImage = activity.getResources().getDrawable(drawerImageRes);
mSlider = new SlideDrawable(mDrawerImage);
mSlider.setOffsetBy(1.f / 3);
}
/**
* Synchronize the state of the drawer indicator/affordance with the linked
* DrawerLayout.
*
* <p>
* This should be called from your <code>Activity</code>'s
* {@link Activity#onPostCreate(android.os.Bundle) onPostCreate} method to
* synchronize after the DrawerLayout's instance state has been restored,
* and any other time when the state may have diverged in such a way that
* the ActionBarDrawerToggle was not notified. (For example, if you stop
* forwarding appropriate drawer events for a period of time.)
* </p>
*/
public void syncState() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mSlider.setOffset(1.f);
} else {
mSlider.setOffset(0.f);
}
if (mDrawerIndicatorEnabled) {
mSetIndicatorInfo = IMPL
.setActionBarUpIndicator(
mSetIndicatorInfo,
mActivity,
mSlider,
mDrawerLayout.isDrawerOpen(GravityCompat.START) ? mOpenDrawerContentDescRes
: mCloseDrawerContentDescRes);
}
}
/**
* Enable or disable the drawer indicator. The indicator defaults to
* enabled.
*
* <p>
* When the indicator is disabled, the <code>ActionBar</code> will revert to
* displaying the home-as-up indicator provided by the <code>Activity</code>
* 's theme in the <code>android.R.attr.homeAsUpIndicator</code> attribute
* instead of the animated drawer glyph.
* </p>
*
* @param enable
* true to enable, false to disable
*/
public void setDrawerIndicatorEnabled(boolean enable) {
if (enable != mDrawerIndicatorEnabled) {
if (enable) {
mSetIndicatorInfo = IMPL
.setActionBarUpIndicator(
mSetIndicatorInfo,
mActivity,
mSlider,
mDrawerLayout.isDrawerOpen(GravityCompat.START) ? mOpenDrawerContentDescRes
: mCloseDrawerContentDescRes);
} else {
mSetIndicatorInfo = IMPL.setActionBarUpIndicator(
mSetIndicatorInfo, mActivity, mThemeImage, 0);
}
mDrawerIndicatorEnabled = enable;
}
}
/**
* @return true if the enhanced drawer indicator is enabled, false otherwise
* @see #setDrawerIndicatorEnabled(boolean)
*/
public boolean isDrawerIndicatorEnabled() {
return mDrawerIndicatorEnabled;
}
/**
* This method should always be called by your <code>Activity</code>'s
* {@link Activity#onConfigurationChanged(android.content.res.Configuration)
* onConfigurationChanged} method.
*
* @param newConfig
* The new configuration
*/
public void onConfigurationChanged(Configuration newConfig) {
// Reload drawables that can change with configuration
mThemeImage = IMPL.getThemeUpIndicator(mActivity);
mDrawerImage = mActivity.getResources().getDrawable(
mDrawerImageResource);
syncState();
}
/**
* This method should be called by your <code>Activity</code>'s
* {@link Activity#onOptionsItemSelected(android.view.MenuItem)
* onOptionsItemSelected} method. If it returns true, your
* <code>onOptionsItemSelected</code> method should return true and skip
* further processing.
*
* @param item
* the MenuItem instance representing the selected menu item
* @return true if the event was handled and further processing should not
* occur
*/
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == ID_HOME
&& mDrawerIndicatorEnabled) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {