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_keepalive),
95 getString(R.string.title_template), }));
96 mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
98 return mDrawerListView;
101 public void setUp(int fragmentId, DrawerLayout drawerLayout) {
102 mFragmentContainerView = getActivity().findViewById(fragmentId);
103 mDrawerLayout = drawerLayout;
105 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
106 GravityCompat.START);
108 ActionBar actionBar = getActionBar();
109 actionBar.setDisplayHomeAsUpEnabled(true);
110 actionBar.setHomeButtonEnabled(true);
111 mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
113 R.drawable.ic_drawer,
114 R.string.navigation_drawer_open,
115 R.string.navigation_drawer_close) {
117 public void onDrawerClosed(View drawerView) {
118 super.onDrawerClosed(drawerView);
123 getActivity().invalidateOptionsMenu();
127 public void onDrawerOpened(View drawerView) {
128 super.onDrawerOpened(drawerView);
133 getActivity().invalidateOptionsMenu();
137 mDrawerLayout.post(new Runnable() {
140 mDrawerToggle.syncState();
144 mDrawerLayout.setDrawerListener(mDrawerToggle);
147 private void selectItem(int position) {
148 mCurrentSelectedPosition = position;
149 if (mDrawerListView != null) {
150 mDrawerListView.setItemChecked(position, true);
152 if (mDrawerLayout != null) {
153 mDrawerLayout.closeDrawer(mFragmentContainerView);
155 if (mCallbacks != null) {
156 mCallbacks.onDrawerItemSelected(position);
161 public void onAttach(Activity activity) {
162 super.onAttach(activity);
164 mCallbacks = (DrawerCallbacks) activity;
165 } catch (ClassCastException e) {
166 throw new ClassCastException("Activity must implement DrawerCallbacks.");
171 public void onDetach() {
177 public void onSaveInstanceState(Bundle outState) {
178 super.onSaveInstanceState(outState);
179 outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
183 public void onConfigurationChanged(Configuration newConfig) {
184 super.onConfigurationChanged(newConfig);
185 mDrawerToggle.onConfigurationChanged(newConfig);
189 public boolean onOptionsItemSelected(MenuItem item) {
190 if (mDrawerToggle.onOptionsItemSelected(item)) {
194 return super.onOptionsItemSelected(item);
197 private ActionBar getActionBar() {
198 return getActivity().getActionBar();
201 public static interface DrawerCallbacks {
202 void onDrawerItemSelected(int position);