2 * ******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base.examples;
25 import android.app.ActionBar;
26 import android.app.Activity;
27 import android.app.Fragment;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.support.v4.app.ActionBarDrawerToggle;
31 import android.support.v4.view.GravityCompat;
32 import android.support.v4.widget.DrawerLayout;
33 import android.view.LayoutInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.AdapterView;
38 import android.widget.ArrayAdapter;
39 import android.widget.ListView;
42 * Through a drawer able to move to another menu.
44 public class DrawerFragment extends Fragment {
46 private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
47 private DrawerCallbacks mCallbacks;
49 private ActionBarDrawerToggle mDrawerToggle;
51 private DrawerLayout mDrawerLayout;
52 private ListView mDrawerListView;
53 private View mFragmentContainerView;
55 private int mCurrentSelectedPosition = 0;
58 public void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
61 if (savedInstanceState != null) {
62 mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
65 selectItem(mCurrentSelectedPosition);
69 public void onActivityCreated(Bundle savedInstanceState) {
70 super.onActivityCreated(savedInstanceState);
71 setHasOptionsMenu(true);
75 public View onCreateView(LayoutInflater inflater, ViewGroup container,
76 Bundle savedInstanceState) {
77 mDrawerListView = (ListView) inflater.inflate(R.layout.fragment_drawer,
80 .setOnItemClickListener(new AdapterView.OnItemClickListener() {
82 public void onItemClick(AdapterView<?> parent, View view,
83 int position, long id) {
87 mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
89 android.R.layout.simple_list_item_activated_1,
90 android.R.id.text1, new String[] {
91 getString(R.string.title_message),
92 getString(R.string.title_bluetooth),
93 getString(R.string.title_cloud),
94 getString(R.string.title_template), }));
95 mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
97 return mDrawerListView;
100 public void setUp(int fragmentId, DrawerLayout drawerLayout) {
101 mFragmentContainerView = getActivity().findViewById(fragmentId);
102 mDrawerLayout = drawerLayout;
104 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
105 GravityCompat.START);
107 ActionBar actionBar = getActionBar();
108 actionBar.setDisplayHomeAsUpEnabled(true);
109 actionBar.setHomeButtonEnabled(true);
110 mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
112 R.drawable.ic_drawer,
113 R.string.navigation_drawer_open,
114 R.string.navigation_drawer_close) {
116 public void onDrawerClosed(View drawerView) {
117 super.onDrawerClosed(drawerView);
122 getActivity().invalidateOptionsMenu();
126 public void onDrawerOpened(View drawerView) {
127 super.onDrawerOpened(drawerView);
132 getActivity().invalidateOptionsMenu();
136 mDrawerLayout.post(new Runnable() {
139 mDrawerToggle.syncState();
143 mDrawerLayout.setDrawerListener(mDrawerToggle);
146 private void selectItem(int position) {
147 mCurrentSelectedPosition = position;
148 if (mDrawerListView != null) {
149 mDrawerListView.setItemChecked(position, true);
151 if (mDrawerLayout != null) {
152 mDrawerLayout.closeDrawer(mFragmentContainerView);
154 if (mCallbacks != null) {
155 mCallbacks.onDrawerItemSelected(position);
160 public void onAttach(Activity activity) {
161 super.onAttach(activity);
163 mCallbacks = (DrawerCallbacks) activity;
164 } catch (ClassCastException e) {
165 throw new ClassCastException("Activity must implement DrawerCallbacks.");
170 public void onDetach() {
176 public void onSaveInstanceState(Bundle outState) {
177 super.onSaveInstanceState(outState);
178 outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
182 public void onConfigurationChanged(Configuration newConfig) {
183 super.onConfigurationChanged(newConfig);
184 mDrawerToggle.onConfigurationChanged(newConfig);
188 public boolean onOptionsItemSelected(MenuItem item) {
189 if (mDrawerToggle.onOptionsItemSelected(item)) {
193 return super.onOptionsItemSelected(item);
196 private ActionBar getActionBar() {
197 return getActivity().getActionBar();
200 public static interface DrawerCallbacks {
201 void onDrawerItemSelected(int position);