Commit eaace2b1 authored by lambroslambrou's avatar lambroslambrou Committed by Commit bot

Android Chromoting: Disable hamburger animation.

This is a little complex, for reasons that are explained in the code
comments. Also, this refactors ChromotingUtil to make it easier to tint
arbitrary Drawables.

Review URL: https://codereview.chromium.org/1461893002

Cr-Commit-Position: refs/heads/master@{#361240}
parent f310b30f
......@@ -11,12 +11,15 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
......@@ -26,6 +29,7 @@ import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Log;
import org.chromium.chromoting.accountswitcher.AccountSwitcher;
import org.chromium.chromoting.accountswitcher.AccountSwitcherFactory;
......@@ -185,10 +189,44 @@ public class Chromoting extends AppCompatActivity implements JniInterface.Connec
findViewById(R.id.host_setup_link_android).setOnClickListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.open_navigation_drawer, R.string.close_navigation_drawer);
mDrawerLayout.setDrawerListener(mDrawerToggle);
// Disable the hamburger icon animation. This is more complex than it ought to be.
// The animation can be customized by tweaking some style parameters - see
// http://developer.android.com/reference/android/support/v7/appcompat/R.styleable.html#DrawerArrowToggle .
// But these can't disable the animation completely.
// The icon can only be changed by disabling the drawer indicator, which has side-effects
// that must be worked around. It disables the built-in click listener, so this has to be
// implemented and added. This also requires that the toolbar be passed to the
// ActionBarDrawerToggle ctor above (otherwise the listener is ignored and warnings are
// logged).
// Also, the animation itself is a private implementation detail - it is not possible to
// simply access the first frame of the animation. And the hamburger menu icon doesn't
// exist as a builtin Android resource, so it has to be provided as an application
// resource instead (R.drawable.ic_menu). And, on Lollipop devices and above, it should be
// tinted to match the colorControlNormal theme attribute.
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.setToolbarNavigationClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
mDrawerLayout.closeDrawer(Gravity.START);
} else {
mDrawerLayout.openDrawer(Gravity.START);
}
}
});
// Set the three-line icon instead of the default which is a tinted arrow icon.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Drawable menuIcon = ApiCompatibilityUtils.getDrawable(getResources(), R.drawable.ic_menu);
DrawableCompat.setTint(menuIcon.mutate(),
ChromotingUtil.getColorAttribute(this, R.attr.colorControlNormal));
getSupportActionBar().setHomeAsUpIndicator(menuIcon);
ListView navigationMenu = new ListView(this);
navigationMenu.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
navigationMenu.setLayoutParams(new LinearLayout.LayoutParams(
......@@ -211,9 +249,6 @@ public class Chromoting extends AppCompatActivity implements JniInterface.Connec
}
});
// Make the navigation drawer icon visible in the ActionBar.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAccountSwitcher = AccountSwitcherFactory.getInstance().createAccountSwitcher(this, this);
mAccountSwitcher.setNavigation(navigationMenu);
LinearLayout navigationDrawer = (LinearLayout) findViewById(R.id.navigation_drawer);
......
......@@ -5,13 +5,13 @@
package org.chromium.chromoting;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Menu;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Log;
/** Utility methods for chromoting code. */
public abstract class ChromotingUtil {
......@@ -24,32 +24,38 @@ public abstract class ChromotingUtil {
* @param menu Menu with icons to be tinted.
*/
public static void tintMenuIcons(Context context, Menu menu) {
int color = getColorAttribute(context, R.attr.colorControlNormal);
int items = menu.size();
for (int i = 0; i < items; i++) {
Drawable icon = menu.getItem(i).getIcon();
if (icon != null) {
icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
}
/**
* Returns a color from a theme attribute.
* @param context Context with resources to look up.
* @param attribute Attribute such as R.attr.colorControlNormal.
* @return Color value.
* @throws Resources.NotFoundException
*/
public static int getColorAttribute(Context context, int attribute) {
TypedValue typedValue = new TypedValue();
if (!context.getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true)) {
Log.e(TAG, "Failed to resolve colorControlNormal attribute.");
return;
if (!context.getTheme().resolveAttribute(attribute, typedValue, true)) {
throw new Resources.NotFoundException("Attribute not found.");
}
int color;
if (typedValue.resourceId != 0) {
// Attribute is a resource.
color = ApiCompatibilityUtils.getColor(context.getResources(), typedValue.resourceId);
return ApiCompatibilityUtils.getColor(context.getResources(), typedValue.resourceId);
} else if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT
&& typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
// Attribute is a raw color value.
color = typedValue.data;
return typedValue.data;
} else {
// The resource compiler should prevent this from happening.
Log.e(TAG, "Invalid colorControlNormal attribute: %s", typedValue);
return;
}
int items = menu.size();
for (int i = 0; i < items; i++) {
Drawable icon = menu.getItem(i).getIcon();
if (icon != null) {
icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
throw new Resources.NotFoundException("Attribute not a color.");
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment