aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/filemanager/ui/widgets/ActionBarDrawerToggle.java
blob: 4ef9e4830fbf2fe06f1117f85ff874cb7363a4c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * 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.
 *
 *
 * (modified from android.support.v4.app)
 */

package com.cyanogenmod.filemanager.ui.widgets;

import java.lang.reflect.Method;

import android.R;
import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * 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 ActionBarDrawerToggle implements DrawerLayout.DrawerListener {
    private static final String TAG = "ActionBarDrawerToggle";

    /**
     * Allows an implementing Activity to return an
     * {@link ActionBarDrawerToggle.Delegate} to use with ActionBarDrawerToggle.
     */
    public interface DelegateProvider {

        /**
         * @return Delegate to use for ActionBarDrawableToggles, or null if the
         *         Activity does not wish to override the default behavior.
         */
        Delegate getDrawerToggleDelegate();
    }

    public interface Delegate {
        /**
         * @return Up indicator drawable as defined in the Activity's theme, or
         *         null if one is not defined.
         */
        Drawable getThemeUpIndicator();

        /**
         * Set the Action Bar's up indicator drawable and content description.
         *
         * @param upDrawable
         *            - Drawable to set as up indicator
         * @param contentDescRes
         *            - Content description to set
         */
        void setActionBarUpIndicator(Drawable upDrawable, int contentDescRes);

        /**
         * Set the Action Bar's up indicator content description.
         *
         * @param contentDescRes
         *            - Content description to set
         */
        void setActionBarDescription(int contentDescRes);
    }

    private static final int[] THEME_ATTRS = new int[] { R.attr.homeAsUpIndicator };

    private static class ActionBarDrawerToggleImpl {
        public static Drawable getThemeUpIndicator(Activity activity) {
            final TypedArray a = activity.obtainStyledAttributes(THEME_ATTRS);
            final Drawable result = a.getDrawable(0);
            a.recycle();
            return result;
        }

        public static Object setActionBarUpIndicator(Object info,
                Activity activity, Drawable drawable, int contentDescRes) {
            if (info == null) {
                info = new SetIndicatorInfo(activity);
            }

            final ActionBar actionBar = activity.getActionBar();
            actionBar.setHomeAsUpIndicator(drawable);
            actionBar.setHomeActionContentDescription(contentDescRes);

            return info;
        }

        public static Object setActionBarDescription(Object info,
                Activity activity, int contentDescRes) {
            if (info == null) {
                info = new SetIndicatorInfo(activity);
            }

            final ActionBar actionBar = activity.getActionBar();
            actionBar.setHomeActionContentDescription(contentDescRes);

            return info;
        }
    }

    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.
                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;
            }
        }
    }

    private static final ActionBarDrawerToggleImpl IMPL = new ActionBarDrawerToggleImpl();

    /** Fraction of its total width by which to offset the toggle drawable. */
    private static final float TOGGLE_DRAWABLE_OFFSET = 1 / 3f;

    // android.R.id.home as defined by public API in v11
    private static final int ID_HOME = 0x0102002c;

    private final Activity mActivity;
    private final Delegate mActivityImpl;
    private final DrawerLayout mDrawerLayout;
    private boolean mDrawerIndicatorEnabled = true;

    private Drawable mThemeImage;
    private Drawable mDrawerImage;
    private SlideDrawable mSlider;
    private 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 ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
            int drawerImageRes, int openDrawerContentDescRes,
            int closeDrawerContentDescRes) {
        mActivity = activity;

        // Allow the Activity to provide an impl
        if (activity instanceof DelegateProvider) {
            mActivityImpl = ((DelegateProvider) activity)
                    .getDrawerToggleDelegate();
        } else {
            mActivityImpl = null;
        }

        mDrawerLayout = drawerLayout;
        mDrawerImageResource = drawerImageRes;
        mOpenDrawerContentDescRes = openDrawerContentDescRes;
        mCloseDrawerContentDescRes = closeDrawerContentDescRes;

        mThemeImage = getThemeUpIndicator();
        mDrawerImage = activity.getResources().getDrawable(drawerImageRes);
        mSlider = new SlideDrawable(mDrawerImage);
        mSlider.setOffset(TOGGLE_DRAWABLE_OFFSET);
    }

    /**
     * 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(Gravity.START)) {
            mSlider.setPosition(1);
        } else {
            mSlider.setPosition(0);
        }

        if (mDrawerIndicatorEnabled) {
            setActionBarUpIndicator(
                    mSlider,
                    mDrawerLayout.isDrawerOpen(Gravity.START) ? mCloseDrawerContentDescRes
                            : mOpenDrawerContentDescRes);
        }
    }

    /**
     * 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) {
                setActionBarUpIndicator(
                        mSlider,
                        mDrawerLayout.isDrawerOpen(Gravity.START) ? mCloseDrawerContentDescRes
                                : mOpenDrawerContentDescRes);
            } else {
                setActionBarUpIndicator(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 replaces the drawer image resource with a new one.
     *
     * @param newDrawerImageRes
     *            The new resource id
     */
    public void setDrawerImageResource(int newDrawerImageRes) {
        mDrawerImageResource = newDrawerImageRes;
        mDrawerImage = mActivity.getResources().getDrawable(
                mDrawerImageResource);
        mSlider = new SlideDrawable(mDrawerImage);
        mSlider.setOffset(TOGGLE_DRAWABLE_OFFSET);
        syncState();
    }

    /**
     * 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 = getThemeUpIndicator();
        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(Gravity.START)) {
                mDrawerLayout.closeDrawer(Gravity.START);
            } else {
                mDrawerLayout.openDrawer(Gravity.START);
            }
            return true;
        }
        return false;
    }

    /**
     * {@link DrawerLayout.DrawerListener} callback method. If you do not use
     * your ActionBarDrawerToggle instance directly as your DrawerLayout's
     * listener, you should call through to this method from your own listener
     * object.
     *
     * @param drawerView
     *            The child view that was moved
     * @param slideOffset
     *            The new offset of this drawer within its range, from 0-1
     */
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        float glyphOffset = mSlider.getPosition();
        if (slideOffset > 0.5f) {
            glyphOffset = Math.max(glyphOffset,
                    Math.max(0.f, slideOffset - 0.5f) * 2);
        } else {
            glyphOffset = Math.min(glyphOffset, slideOffset * 2);
        }
        mSlider.setPosition(glyphOffset);
    }

    /**
     * {@link DrawerLayout.DrawerListener} callback method. If you do not use
     * your ActionBarDrawerToggle instance directly as your DrawerLayout's
     * listener, you should call through to this method from your own listener
     * object.
     *
     * @param drawerView
     *            Drawer view that is now open
     */
    @Override
    public void onDrawerOpened(View drawerView) {
        mSlider.setPosition(1);
        if (mDrawerIndicatorEnabled) {
            setActionBarDescription(mCloseDrawerContentDescRes);
        }
    }

    /**
     * {@link DrawerLayout.DrawerListener} callback method. If you do not use
     * your ActionBarDrawerToggle instance directly as your DrawerLayout's
     * listener, you should call through to this method from your own listener
     * object.
     *
     * @param drawerView
     *            Drawer view that is now closed
     */
    @Override
    public void onDrawerClosed(View drawerView) {
        mSlider.setPosition(0);
        if (mDrawerIndicatorEnabled) {
            setActionBarDescription(mOpenDrawerContentDescRes);
        }
    }

    /**
     * {@link DrawerLayout.DrawerListener} callback method. If you do not use
     * your ActionBarDrawerToggle instance directly as your DrawerLayout's
     * listener, you should call through to this method from your own listener
     * object.
     *
     * @param newState
     *            The new drawer motion state
     */
    @Override
    public void onDrawerStateChanged(int newState) {
    }

    Drawable getThemeUpIndicator() {
        if (mActivityImpl != null) {
            return mActivityImpl.getThemeUpIndicator();
        }
        return IMPL.getThemeUpIndicator(mActivity);
    }

    void setActionBarUpIndicator(Drawable upDrawable, int contentDescRes) {
        if (mActivityImpl != null) {
            mActivityImpl.setActionBarUpIndicator(upDrawable, contentDescRes);
            return;
        }
        mSetIndicatorInfo = IMPL.setActionBarUpIndicator(mSetIndicatorInfo,
                mActivity, upDrawable, contentDescRes);
    }

    void setActionBarDescription(int contentDescRes) {
        if (mActivityImpl != null) {
            mActivityImpl.setActionBarDescription(contentDescRes);
            return;
        }
        mSetIndicatorInfo = IMPL.setActionBarDescription(mSetIndicatorInfo,
                mActivity, contentDescRes);
    }

    private class SlideDrawable extends LevelListDrawable implements
            Drawable.Callback {
        private final boolean mHasMirroring = Build.VERSION.SDK_INT > 18;
        private final Rect mTmpRect = new Rect();

        private float mPosition;
        private float mOffset;

        private SlideDrawable(Drawable wrapped) {
            super();

            if (wrapped.isAutoMirrored()) {
                this.setAutoMirrored(true);
            }

            addLevel(0, 0, wrapped);
        }

        /**
         * Sets the current position along the offset.
         *
         * @param position
         *            a value between 0 and 1
         */
        public void setPosition(float position) {
            mPosition = position;
            invalidateSelf();
        }

        public float getPosition() {
            return mPosition;
        }

        /**
         * Specifies the maximum offset when the position is at 1.
         *
         * @param offset
         *            maximum offset as a fraction of the drawable width,
         *            positive to shift left or negative to shift right.
         * @see #setPosition(float)
         */
        public void setOffset(float offset) {
            mOffset = offset;
            invalidateSelf();
        }

        @Override
        public void draw(Canvas canvas) {
            copyBounds(mTmpRect);
            canvas.save();

            // Layout direction must be obtained from the activity.
            final boolean isLayoutRTL = mActivity.getWindow().getDecorView()
                    .getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
            final int flipRtl = isLayoutRTL ? -1 : 1;
            final int width = mTmpRect.width();
            canvas.translate(-mOffset * width * mPosition * flipRtl, 0);

            // Force auto-mirroring if it's not supported by the platform.
            if (isLayoutRTL && !mHasMirroring) {
                canvas.translate(width, 0);
                canvas.scale(-1, 1);
            }

            super.draw(canvas);
            canvas.restore();
        }
    }
}