replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / examples / simplebase / src / main / java / org / iotivity / base / examples / DrawerFragment.java
1 /*
2  * ******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.base.examples;
24
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;
40
41 /**
42  * Through a drawer able to move to another menu.
43  */
44 public class DrawerFragment extends Fragment {
45
46     private static final String   STATE_SELECTED_POSITION  = "selected_navigation_drawer_position";
47     private DrawerCallbacks       mCallbacks;
48
49     private ActionBarDrawerToggle mDrawerToggle;
50
51     private DrawerLayout          mDrawerLayout;
52     private ListView              mDrawerListView;
53     private View                  mFragmentContainerView;
54
55     private int                   mCurrentSelectedPosition = 0;
56
57     @Override
58     public void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60
61         if (savedInstanceState != null) {
62             mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
63         }
64
65         selectItem(mCurrentSelectedPosition);
66     }
67
68     @Override
69     public void onActivityCreated(Bundle savedInstanceState) {
70         super.onActivityCreated(savedInstanceState);
71         setHasOptionsMenu(true);
72     }
73
74     @Override
75     public View onCreateView(LayoutInflater inflater, ViewGroup container,
76             Bundle savedInstanceState) {
77         mDrawerListView = (ListView) inflater.inflate(R.layout.fragment_drawer,
78                                                       container, false);
79         mDrawerListView
80                 .setOnItemClickListener(new AdapterView.OnItemClickListener() {
81                     @Override
82                     public void onItemClick(AdapterView<?> parent, View view,
83                                             int position, long id) {
84                         selectItem(position);
85                     }
86                 });
87         mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
88                 .getThemedContext(),
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);
97
98         return mDrawerListView;
99     }
100
101     public void setUp(int fragmentId, DrawerLayout drawerLayout) {
102         mFragmentContainerView = getActivity().findViewById(fragmentId);
103         mDrawerLayout = drawerLayout;
104
105         mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
106                                       GravityCompat.START);
107
108         ActionBar actionBar = getActionBar();
109         actionBar.setDisplayHomeAsUpEnabled(true);
110         actionBar.setHomeButtonEnabled(true);
111         mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
112                                                   mDrawerLayout,
113                                                   R.drawable.ic_drawer,
114                                                   R.string.navigation_drawer_open,
115                                                   R.string.navigation_drawer_close) {
116             @Override
117             public void onDrawerClosed(View drawerView) {
118                 super.onDrawerClosed(drawerView);
119                 if (!isAdded()) {
120                     return;
121                 }
122
123                 getActivity().invalidateOptionsMenu();
124             }
125
126             @Override
127             public void onDrawerOpened(View drawerView) {
128                 super.onDrawerOpened(drawerView);
129                 if (!isAdded()) {
130                     return;
131                 }
132
133                 getActivity().invalidateOptionsMenu();
134             }
135         };
136
137         mDrawerLayout.post(new Runnable() {
138             @Override
139             public void run() {
140                 mDrawerToggle.syncState();
141             }
142         });
143
144         mDrawerLayout.setDrawerListener(mDrawerToggle);
145     }
146
147     private void selectItem(int position) {
148         mCurrentSelectedPosition = position;
149         if (mDrawerListView != null) {
150             mDrawerListView.setItemChecked(position, true);
151         }
152         if (mDrawerLayout != null) {
153             mDrawerLayout.closeDrawer(mFragmentContainerView);
154         }
155         if (mCallbacks != null) {
156             mCallbacks.onDrawerItemSelected(position);
157         }
158     }
159
160     @Override
161     public void onAttach(Activity activity) {
162         super.onAttach(activity);
163         try {
164             mCallbacks = (DrawerCallbacks) activity;
165         } catch (ClassCastException e) {
166             throw new ClassCastException("Activity must implement DrawerCallbacks.");
167         }
168     }
169
170     @Override
171     public void onDetach() {
172         super.onDetach();
173         mCallbacks = null;
174     }
175
176     @Override
177     public void onSaveInstanceState(Bundle outState) {
178         super.onSaveInstanceState(outState);
179         outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
180     }
181
182     @Override
183     public void onConfigurationChanged(Configuration newConfig) {
184         super.onConfigurationChanged(newConfig);
185         mDrawerToggle.onConfigurationChanged(newConfig);
186     }
187
188     @Override
189     public boolean onOptionsItemSelected(MenuItem item) {
190         if (mDrawerToggle.onOptionsItemSelected(item)) {
191             return true;
192         }
193
194         return super.onOptionsItemSelected(item);
195     }
196
197     private ActionBar getActionBar() {
198         return getActivity().getActionBar();
199     }
200
201     public static interface DrawerCallbacks {
202         void onDrawerItemSelected(int position);
203     }
204 }