aboutsummaryrefslogtreecommitdiffstats
path: root/iget_android/app/src/main/java/com/iget/ccnx/DrawerFragment.java
blob: 72960fc50bd24b3d2570549d9e48931d43433158 (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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.iget.ccnx;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * DrawerFragment that provides navigation for MainActivity.
 */
public class DrawerFragment extends Fragment {

  public static DrawerFragment
  newInstance(ArrayList<DrawerFragment.DrawerItem> items) {
    Bundle drawerParams = new Bundle();
    drawerParams.putParcelableArrayList(DrawerFragment.BUNDLE_PARAMETERS, items);

    DrawerFragment fragment = new DrawerFragment();
    fragment.setArguments(drawerParams);
    return fragment;
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
      // Register callback
      m_callbacks = (DrawerCallbacks)activity;
    } catch (ClassCastException e) {
      throw new ClassCastException("Host activity must implement DrawerFragment.DrawerCallbacks.");
    }
  }

  @Override
  public void onDetach() {
    super.onDetach();
    m_callbacks = null;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Read in the flag indicating whether or not the user has demonstrated awareness of the
    // drawer. See PREF_DRAWER_SHOWN_TO_USER_FOR_THE_FIRST_TIME for details.
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
    m_hasUserSeenDrawer = sp.getBoolean(PREF_DRAWER_SHOWN_TO_USER_FOR_THE_FIRST_TIME, false);

    if (savedInstanceState != null) {
      m_drawerSelectedPosition = savedInstanceState.getInt(DRAWER_SELECTED_POSITION_BUNDLE_KEY);
      m_restoredFromSavedInstanceState = true;
    }

    m_drawerItems = getArguments().getParcelableArrayList(BUNDLE_PARAMETERS);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState)
  {
    m_drawerListView = (ListView)inflater.inflate(
      R.layout.activity_main_drawer_listview, container, false);

    m_drawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Update UI
        updateSelection(position);
      }
    });

    m_drawerListView.setAdapter(new DrawerListAdapter(getActionBar().getThemedContext(), m_drawerItems));
    m_drawerListView.setItemChecked(m_drawerSelectedPosition, true);

    return m_drawerListView;
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Fragment influences action bar
    setHasOptionsMenu(true);

    // Initialize and set up the navigation drawer UI
    initializeDrawerFragment(getActivity().findViewById(R.id.navigation_drawer),
                             (DrawerLayout)getActivity().findViewById(R.id.drawer_layout));

    if (savedInstanceState == null) {
      // when restoring (e.g., after rotation), rely on system to restore previous state of
      // fragments
      updateSelection(m_drawerSelectedPosition);
    }
  }

  /**
   * Initialize drawer fragment after being attached to the host activity.
   *
   * @param drawerFragmentViewContainer View container that holds the navigation drawer
   * @param drawerLayout DrawerLayout of the drawer in the host Activity
   */
  private void initializeDrawerFragment(View drawerFragmentViewContainer,
                                        DrawerLayout drawerLayout)
  {
    m_drawerFragmentViewContainer = drawerFragmentViewContainer;
    m_drawerLayout = drawerLayout;

    // Setup drawer and action bar
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    m_drawerToggle = new ActionBarDrawerToggle(
        getActivity(),
        m_drawerLayout,
        R.string.accessibility_open_drawer,
        R.string.accessibility_close_drawer)
    {
      @Override
      public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);

        // Allow update calls to onCreateOptionsMenu() and
        // onPrepareOptionsMenu() to update Menu UI.
        m_shouldHideOptionsMenu = false;
        getActivity().supportInvalidateOptionsMenu();
      }

      @Override
      public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);

        // Flag that user has seen drawer for the first time
        if (!m_hasUserSeenDrawer) {
          m_hasUserSeenDrawer = true;
          SharedPreferences sp = PreferenceManager
              .getDefaultSharedPreferences(getActivity());

          sp.edit()
            .putBoolean(PREF_DRAWER_SHOWN_TO_USER_FOR_THE_FIRST_TIME, true)
            .apply();
        }

        // Allow update calls to onCreateOptionsMenu() and
        // onPrepareOptionsMenu() to update Menu UI
        m_shouldHideOptionsMenu = true;
        getActivity().supportInvalidateOptionsMenu();
      }

      @Override
      public void onDrawerStateChanged(int newState) {
        super.onDrawerStateChanged(newState);
        if (newState != ViewDragHelper.STATE_IDLE) {
          // opened/closed is handled by onDrawerOpened and onDrawerClosed callbacks
          m_shouldHideOptionsMenu = true;
          getActivity().supportInvalidateOptionsMenu();
        } else if (newState == ViewDragHelper.STATE_IDLE && !isDrawerOpen()) {
          // This condition takes care of the case of displaying the option menu
          // items when the drawer is retracted prematurely.
          m_shouldHideOptionsMenu = false;
          getActivity().supportInvalidateOptionsMenu();
        }
      }
    };

    // Open drawer for the first time
    if (!m_hasUserSeenDrawer && !m_restoredFromSavedInstanceState) {
      m_shouldHideOptionsMenu = true;
      m_drawerLayout.openDrawer(m_drawerFragmentViewContainer);
    }

    // Post to drawer's handler to update UI State
    m_drawerLayout.post(new Runnable() {
      @Override
      public void run() {
        m_drawerToggle.syncState();
      }
    });

    m_drawerLayout.setDrawerListener(m_drawerToggle);
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(DRAWER_SELECTED_POSITION_BUNDLE_KEY, m_drawerSelectedPosition);
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Forward the new configuration the drawer toggle component.
    m_drawerToggle.onConfigurationChanged(newConfig);
  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    // Update menu UI when the drawer is open. This gives the user a better
    // contextual perception of the application.
    if (isDrawerOpen()) {
      // Inflate drawer specific menu here (if any)
      showGlobalContextActionBar();
    }

  }

  @Override
  public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    // Remove option menu items when drawer is sliding out
    if (m_shouldHideOptionsMenu) {
      for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setVisible(false);
      }
    }

  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle drawer selection events
    if (m_drawerToggle.onOptionsItemSelected(item)) {
      return true;
    }

    // Handle other menu items
    switch (item.getItemId()) {
    // Handle activity menu item here (if any)
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  public boolean
  shouldHideOptionsMenu() {
    return m_shouldHideOptionsMenu;
  }

  /**
   * Convenience method that updates the UI and callbacks the host activity.
   *
   * @param position Position of the selected item within the Drawer's ListView.
   */
  private void updateSelection(int position) {
    // Update Position
    m_drawerSelectedPosition = position;

    // Update UI of selected position
    if (m_drawerListView != null) {
      m_drawerListView.setItemChecked(position, true);
    }

    // Close drawer
    if (m_drawerLayout != null) {
      m_drawerLayout.closeDrawer(m_drawerFragmentViewContainer);
    }

    // Invoke host activity callback
    if (m_callbacks != null) {
      DrawerItem item = m_drawerItems.get(position);
      m_callbacks.onDrawerItemSelected(item.getItemCode(), item.getItemName());
    }
  }

  /**
   * Safe convenience method to determine if drawer is open.
   *
   * @return True if drawer is present and in an open state; false otherwise
   */
  private boolean
  isDrawerOpen() {
    return m_drawerLayout != null && m_drawerLayout.isDrawerOpen(m_drawerFragmentViewContainer);
  }

  /**
   * Convenience method to update host activity action bar so that the user is informed of
   * the app's "current context" of the fragment.
   */
  private void showGlobalContextActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(R.string.app_name);
  }

  /**
   * Convenience method to get host activity's ActionBar. This makes for easy updates
   * in a single location when upgrading to use >= HONEYCOMB (API 11) ActionBar.
   *
   * @return Host activity's ActionBar.
   */
  private ActionBar getActionBar() {
    return ((ActionBarActivity)getActivity()).getSupportActionBar();
  }

  //////////////////////////////////////////////////////////////////////////////

  /**
   * DrawerItem represents a single selection option in the Drawer, complete
   * with the ability to set a Drawable resource icon for display along
   * with the drawer item name.
   */
  public static class DrawerItem implements Parcelable {
    @Override
    public int describeContents()
    {
      return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i)
    {
      parcel.writeInt(m_itemNameId);
      parcel.writeInt(m_iconResId);
      parcel.writeInt(m_itemCode);
    }

    public static final Parcelable.Creator<DrawerItem> CREATOR = new Parcelable.Creator<DrawerItem>() {
      public DrawerItem
      createFromParcel(Parcel in) {
        return new DrawerItem(in.readInt(), in.readInt(), in.readInt());
      }

      public DrawerItem[] newArray(int size) {
        return new DrawerItem[size];
      }
    };

    public
    DrawerItem(int itemNameId, int resIconId, int itemCode) {
      m_itemNameId = itemNameId;
      m_iconResId = resIconId;
      m_itemCode = itemCode;
    }

    public int
    getItemName() {
      return m_itemNameId;
    }

    public int
    getIconResId() {
      return m_iconResId;
    }

    public int
    getItemCode() {
      return m_itemCode;
    }

    ///////////////////////////////////////////////////////////////////////////

    /** Drawer item name */
    private final int m_itemNameId;

    /** Resource ID of a drawable to be shown as the item's icon */
    private final int m_iconResId;

    /** Item code for feedback to the host activity's implemented callback. */
    private final int m_itemCode;

  }

  /**
   * Customized DrawerListAdapter to furnishes the Drawer with DrawerItem
   * information.
   */
  private static class DrawerListAdapter extends ArrayAdapter<DrawerItem> {

    public DrawerListAdapter(Context context, ArrayList<DrawerItem> drawerItems) {
      super(context, 0, drawerItems);
      m_layoutInflater =
          (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      m_resources = context.getResources();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      DrawerItemHolder holder;

      if (convertView == null) {
        holder = new DrawerItemHolder();

        convertView = m_layoutInflater.inflate(R.layout.list_item_drawer_item, null);
        convertView.setTag(holder);

        holder.m_icon = (ImageView) convertView.findViewById(R.id.drawer_item_icon);
        holder.m_text = (TextView) convertView.findViewById(R.id.drawer_item_text);
      } else {
        holder = (DrawerItemHolder)convertView.getTag();
      }

      // Update items in holder
      DrawerItem item = getItem(position);
      if (item.getIconResId() != 0) {
        holder.m_icon.setImageDrawable(m_resources.getDrawable(item.getIconResId()));
      }
      holder.m_text.setText(item.getItemName());

      return convertView;
    }

    private static class DrawerItemHolder {
      private ImageView m_icon;
      private TextView m_text;
    }

    /** Layout inflater for use */
    private final LayoutInflater m_layoutInflater;

    /** Reference to get context's resources */
    private final Resources m_resources;
  }

  //////////////////////////////////////////////////////////////////////////////

  /** Callback that host activity must implement */
  public static interface DrawerCallbacks {
    /** Callback to host activity when a drawer item is selected */
    void onDrawerItemSelected(int itemCode, int itemNameId);
  }

  //////////////////////////////////////////////////////////////////////////////

  /** SharedPreference: Display drawer when drawer loads for the very first time */
  private static final String PREF_DRAWER_SHOWN_TO_USER_FOR_THE_FIRST_TIME
      = "DRAWER_PRESENTED_TO_USER_ON_FIRST_LOAD";

  /** Bundle key used to (re)store position of selected drawer item */
  private static final String DRAWER_SELECTED_POSITION_BUNDLE_KEY
      = "DRAWER_SELECTED_POSITION";

  /** Bundle argument key for bundle parameters */
  private static final String BUNDLE_PARAMETERS = "net.named_data.nfd.drawer_fragment_parameters";

  /** Callback to parent activity */
  private DrawerCallbacks m_callbacks;

  /** DrawerToggle for interacting with drawer and action bar app icon */
  private ActionBarDrawerToggle m_drawerToggle;

  /** Reference to DrawerLayout fragment in host activity */
  private DrawerLayout m_drawerLayout;

  /** Reference to drawer's ListView */
  private ListView m_drawerListView;

  /** Drawer's fragment container in the host activity */
  private View m_drawerFragmentViewContainer;

  /** Current position of the Drawer's selection */
  private int m_drawerSelectedPosition = 0;

  /** Flag that denotes if the fragment is restored from an instance state */
  private boolean m_restoredFromSavedInstanceState;

  /** Flag that denotes if the user has seen the Drawer when the app loads for the first time */
  private boolean m_hasUserSeenDrawer;

  /** ArrayList of DrawerItems to be displayed in the Drawer */
  private ArrayList<DrawerItem> m_drawerItems;

  /** Flag that marks if drawer is sliding outwards and being displayed */
  private boolean m_shouldHideOptionsMenu = false;
}