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_template), }));
94 mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
96 return mDrawerListView;
99 public void setUp(int fragmentId, DrawerLayout drawerLayout) {
100 mFragmentContainerView = getActivity().findViewById(fragmentId);
101 mDrawerLayout = drawerLayout;
103 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
104 GravityCompat.START);
106 ActionBar actionBar = getActionBar();
107 actionBar.setDisplayHomeAsUpEnabled(true);
108 actionBar.setHomeButtonEnabled(true);
109 mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
111 R.drawable.ic_drawer,
112 R.string.navigation_drawer_open,
113 R.string.navigation_drawer_close) {
115 public void onDrawerClosed(View drawerView) {
116 super.onDrawerClosed(drawerView);
121 getActivity().invalidateOptionsMenu();
125 public void onDrawerOpened(View drawerView) {
126 super.onDrawerOpened(drawerView);
131 getActivity().invalidateOptionsMenu();
135 mDrawerLayout.post(new Runnable() {
138 mDrawerToggle.syncState();
142 mDrawerLayout.setDrawerListener(mDrawerToggle);
145 private void selectItem(int position) {
146 mCurrentSelectedPosition = position;
147 if (mDrawerListView != null) {
148 mDrawerListView.setItemChecked(position, true);
150 if (mDrawerLayout != null) {
151 mDrawerLayout.closeDrawer(mFragmentContainerView);
153 if (mCallbacks != null) {
154 mCallbacks.onDrawerItemSelected(position);
159 public void onAttach(Activity activity) {
160 super.onAttach(activity);
162 mCallbacks = (DrawerCallbacks) activity;
163 } catch (ClassCastException e) {
164 throw new ClassCastException("Activity must implement DrawerCallbacks.");
169 public void onDetach() {
175 public void onSaveInstanceState(Bundle outState) {
176 super.onSaveInstanceState(outState);
177 outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
181 public void onConfigurationChanged(Configuration newConfig) {
182 super.onConfigurationChanged(newConfig);
183 mDrawerToggle.onConfigurationChanged(newConfig);
187 public boolean onOptionsItemSelected(MenuItem item) {
188 if (mDrawerToggle.onOptionsItemSelected(item)) {
192 return super.onOptionsItemSelected(item);
195 private ActionBar getActionBar() {
196 return getActivity().getActionBar();
199 public static interface DrawerCallbacks {
200 void onDrawerItemSelected(int position);