disable cloud function in simplebase app.
[platform/upstream/iotivity.git] / java / examples-android / 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_template), }));
94         mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
95
96         return mDrawerListView;
97     }
98
99     public void setUp(int fragmentId, DrawerLayout drawerLayout) {
100         mFragmentContainerView = getActivity().findViewById(fragmentId);
101         mDrawerLayout = drawerLayout;
102
103         mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
104                                       GravityCompat.START);
105
106         ActionBar actionBar = getActionBar();
107         actionBar.setDisplayHomeAsUpEnabled(true);
108         actionBar.setHomeButtonEnabled(true);
109         mDrawerToggle = new ActionBarDrawerToggle(getActivity(),
110                                                   mDrawerLayout,
111                                                   R.drawable.ic_drawer,
112                                                   R.string.navigation_drawer_open,
113                                                   R.string.navigation_drawer_close) {
114             @Override
115             public void onDrawerClosed(View drawerView) {
116                 super.onDrawerClosed(drawerView);
117                 if (!isAdded()) {
118                     return;
119                 }
120
121                 getActivity().invalidateOptionsMenu();
122             }
123
124             @Override
125             public void onDrawerOpened(View drawerView) {
126                 super.onDrawerOpened(drawerView);
127                 if (!isAdded()) {
128                     return;
129                 }
130
131                 getActivity().invalidateOptionsMenu();
132             }
133         };
134
135         mDrawerLayout.post(new Runnable() {
136             @Override
137             public void run() {
138                 mDrawerToggle.syncState();
139             }
140         });
141
142         mDrawerLayout.setDrawerListener(mDrawerToggle);
143     }
144
145     private void selectItem(int position) {
146         mCurrentSelectedPosition = position;
147         if (mDrawerListView != null) {
148             mDrawerListView.setItemChecked(position, true);
149         }
150         if (mDrawerLayout != null) {
151             mDrawerLayout.closeDrawer(mFragmentContainerView);
152         }
153         if (mCallbacks != null) {
154             mCallbacks.onDrawerItemSelected(position);
155         }
156     }
157
158     @Override
159     public void onAttach(Activity activity) {
160         super.onAttach(activity);
161         try {
162             mCallbacks = (DrawerCallbacks) activity;
163         } catch (ClassCastException e) {
164             throw new ClassCastException("Activity must implement DrawerCallbacks.");
165         }
166     }
167
168     @Override
169     public void onDetach() {
170         super.onDetach();
171         mCallbacks = null;
172     }
173
174     @Override
175     public void onSaveInstanceState(Bundle outState) {
176         super.onSaveInstanceState(outState);
177         outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
178     }
179
180     @Override
181     public void onConfigurationChanged(Configuration newConfig) {
182         super.onConfigurationChanged(newConfig);
183         mDrawerToggle.onConfigurationChanged(newConfig);
184     }
185
186     @Override
187     public boolean onOptionsItemSelected(MenuItem item) {
188         if (mDrawerToggle.onOptionsItemSelected(item)) {
189             return true;
190         }
191
192         return super.onOptionsItemSelected(item);
193     }
194
195     private ActionBar getActionBar() {
196         return getActivity().getActionBar();
197     }
198
199     public static interface DrawerCallbacks {
200         void onDrawerItemSelected(int position);
201     }
202 }