1d311c9830e588a36a4c8215b4d056d9c2a24c3a
[mobilnebezpieczenstwo.git] / src / com / samsung / srpol / ui / MainActivity.java
1 /*
2    Copyright (C) 2014  Samsung Electronics Polska Sp. z o.o.
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU AFFERO General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8     You may obtain a copy of the License at
9
10                 http://www.gnu.org/licenses/agpl-3.0.txt
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 package com.samsung.srpol.ui;
22
23 import java.util.List;
24
25 import com.samsung.srpol.R;
26 import com.samsung.srpol.loader.AppDetails;
27 import com.samsung.srpol.loader.AppListLoader;
28 import com.samsung.srpol.ui.drawer.MenuFragment;
29 import com.samsung.srpol.ui.drawer.NavigationDrawerItemListener;
30 import com.samsung.srpol.ui.tabpager.PageAdapter;
31
32 import android.support.v7.app.ActionBarActivity;
33 import android.support.v7.app.ActionBar;
34 import android.support.v4.app.ActionBarDrawerToggle;
35 import android.support.v4.app.LoaderManager;
36 import android.support.v4.content.Loader;
37 import android.content.DialogInterface;
38 import android.content.DialogInterface.OnDismissListener;
39 import android.content.Intent;
40 import android.content.SharedPreferences;
41 import android.os.Bundle;
42 import android.os.Handler;
43 import android.preference.PreferenceManager;
44 import android.util.Log;
45 import android.view.Gravity;
46 import android.view.Menu;
47 import android.view.MenuItem;
48 import android.view.View;
49 import android.support.v4.view.GravityCompat;
50 import android.support.v4.view.PagerTabStrip;
51 import android.support.v4.view.ViewPager;
52 import android.support.v4.widget.DrawerLayout;
53
54 public class MainActivity extends ActionBarActivity implements
55         LoaderManager.LoaderCallbacks<List<AppDetails>> {
56     public static final String TAG = "MainActivity";
57
58     private static final String STATE_WELCOME_DIALOG_IS_OPEN = "welcome_dialog_is_open";
59     private static final String STATE_WELCOME_DIALOG_OPENED_FROM_MENU = "welcome_dialog_opened_from_menu";
60     private static final String STATE_DRAWER_MENU_IS_OPEN = "state_drawer_menu_is_open";
61
62     private MenuFragment mMenuFragment;
63
64     private WelcomeDialog mWelcomeDialog;
65     private ViewPager mViewPager;
66     private PageAdapter mFramePagerAdapter;
67     private boolean mDrawerOpenState = false;
68
69     @Override
70     protected void onCreate(Bundle savedInstanceState) {
71         super.onCreate(savedInstanceState);
72         setContentView(R.layout.activity_main);
73
74         if (savedInstanceState != null) {
75             boolean isWelcomeDialogOpen = savedInstanceState.getBoolean(
76                     STATE_WELCOME_DIALOG_IS_OPEN, false);
77             if (isWelcomeDialogOpen) {
78                 boolean isOpenedFromMenu = savedInstanceState.getBoolean(
79                         STATE_WELCOME_DIALOG_OPENED_FROM_MENU, false);
80                 showWelcomeDialog(isOpenedFromMenu);
81             }
82             mDrawerOpenState = savedInstanceState.getBoolean(STATE_DRAWER_MENU_IS_OPEN, false);
83         } else {
84             showWelcomeDialog(false);
85         }
86
87         getSupportLoaderManager().initLoader(0, null, this);
88
89         // if Loader was created then we have all data to create
90         // NavigationDrawer otherwise wait for loader to be created and then
91         // create NavigationDrawer
92         if (getSupportLoaderManager().getLoader(0) != null) {
93             initNavigationDrawer();
94         }
95
96         mFramePagerAdapter = new PageAdapter(getSupportFragmentManager(), getApplicationContext());
97         mViewPager = (ViewPager) findViewById(R.id.myviewpager);
98         mViewPager.setAdapter(mFramePagerAdapter);
99
100         PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.titlestrip);
101         pagerTabStrip.setDrawFullUnderline(true);
102         pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.tab_underline_color));
103     }
104
105     @Override
106     public void onSaveInstanceState(Bundle outState) {
107         super.onSaveInstanceState(outState);
108         if (mWelcomeDialog != null) {
109             outState.putBoolean(STATE_WELCOME_DIALOG_IS_OPEN,
110                     mWelcomeDialog.isShowing());
111             outState.putBoolean(STATE_WELCOME_DIALOG_OPENED_FROM_MENU,
112                     mWelcomeDialog.ismOpenedFromMenu());
113         }
114         if (isDrawerOpen() || (mDrawerOpenState && mDrawerLayout == null)) {
115             outState.putBoolean(STATE_DRAWER_MENU_IS_OPEN, true);
116         }
117     }
118
119     protected void onDestroy() {
120         if (mWelcomeDialog != null) {
121             if (mWelcomeDialog.isShowing()) {
122                 mWelcomeDialog.dismiss();
123             }
124         }
125         super.onDestroy();
126     }
127
128     public void restoreActionBar() {
129         ActionBar actionBar = getSupportActionBar();
130         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
131         actionBar.setDisplayShowTitleEnabled(true);
132     }
133
134     @Override
135     protected boolean onPrepareOptionsPanel(View view, Menu menu) {
136         SharedPreferences sp = PreferenceManager
137                 .getDefaultSharedPreferences(this);
138         boolean showSystemApp = sp.getBoolean(
139                 AppListLoader.PREF_INCLUDE_SYSTEM_APPS,
140                 true);
141         MenuItem menuItemSystem = menu.findItem(R.id.action_toggle_system_visibility);
142         if (menuItemSystem != null) {
143             if (showSystemApp) {
144                 menuItemSystem.setTitle(R.string.hide_system_visibility);
145             } else {
146                 menuItemSystem.setTitle(R.string.show_system_visibility);
147             }
148         }
149         return super.onPrepareOptionsPanel(view, menu);
150     }
151     
152     @Override
153     public boolean onCreateOptionsMenu(Menu menu) {
154         getMenuInflater().inflate(R.menu.main, menu);
155         restoreActionBar();
156         return true;
157     }
158
159     @Override
160     public void onBackPressed() {
161         if (isDrawerOpen()) {
162             closeDrawer();
163         } else {
164             super.onBackPressed();
165         }
166     }
167
168     @Override
169     public boolean onOptionsItemSelected(MenuItem item) {
170         if (mActionBarDrawerToggle != null
171                 && mActionBarDrawerToggle.onOptionsItemSelected(item))
172             return true;
173         int id = item.getItemId();
174         switch (id) {
175             case R.id.action_about:
176                 showWelcomeDialog(true);
177                 return true;
178             case R.id.action_licence:
179                 showLicense();
180                 return true;
181             case R.id.action_toggle_system_visibility:
182                 toggleSharedPrefences(AppListLoader.PREF_INCLUDE_SYSTEM_APPS);
183                 refreshAfterChanges();
184                 return true;
185         }
186         return super.onOptionsItemSelected(item);
187     }
188
189     public void toggleSharedPrefences(String preferenceName) {
190         SharedPreferences sp = PreferenceManager
191                 .getDefaultSharedPreferences(this);
192         boolean actualValue = sp.getBoolean(preferenceName, true);
193         sp.edit().putBoolean(preferenceName, !actualValue).commit();
194     }
195
196     public void refreshAfterChanges() {
197         supportInvalidateOptionsMenu();
198         if (mFramePagerAdapter != null) {
199             mFramePagerAdapter.refreshAdapterNotify();
200             mFramePagerAdapter.notifyDataSetChanged();
201         }
202         if (mMenuFragment != null) {
203             mMenuFragment.notifyDataSetChanged();
204         }
205     }
206
207     @Override
208     public Loader<List<AppDetails>> onCreateLoader(int id, Bundle args) {
209         AppListLoader loader = new AppListLoader(this.getApplicationContext());
210         initNavigationDrawer();
211
212         return loader;
213     }
214
215     @Override
216     public void onLoadFinished(Loader<List<AppDetails>> loader,
217             List<AppDetails> data) {
218         Log.d(TAG, "onLoadFinished");
219
220         AppListLoader appLoader = (AppListLoader) loader;
221         if (appLoader.wasDataReloaded()) {
222             appLoader.resetWasDataReloaded();
223             mViewPager.setOffscreenPageLimit(AppListLoader.getCategories().size());
224             mFramePagerAdapter.refreshPages();
225         } else {
226             mFramePagerAdapter.refreshAdapterNotify();
227         }
228         if (mWelcomeDialog != null && mWelcomeDialog.isShowing()) {
229             mWelcomeDialog.loadingDone();
230         }
231         if (mMenuFragment != null) {
232             mMenuFragment.notifyDataSetChanged();
233         }
234     }
235
236     public void onLoaderReset(Loader<List<AppDetails>> arg0) {
237         mFramePagerAdapter.refreshAdapterNotify();
238     }
239
240     private void showWelcomeDialog(boolean fromMenu) {
241         Log.d(TAG, "showWelcomeActivity flag: " + fromMenu);
242         if (mWelcomeDialog == null) {
243             mWelcomeDialog = new WelcomeDialog(this, fromMenu);
244         }
245         if (!mWelcomeDialog.isShowing()) {
246             mWelcomeDialog.show(fromMenu);
247         }
248         if (!fromMenu) {
249             mWelcomeDialog.setOnDismissListener(new OnDismissListener() {
250
251                 @Override
252                 public void onDismiss(DialogInterface dialog) {
253                     openDrawerAnimate();
254                 }
255             });
256         }
257     }
258
259     private void showLicense() {
260         Intent intent = new Intent(this, LicenseActivity.class);
261         startActivity(intent);
262     }
263
264     private DrawerLayout mDrawerLayout;
265     private View mFragmentContainerView;
266     private ActionBarDrawerToggle mActionBarDrawerToggle;
267
268     private void initNavigationDrawer() {
269         mMenuFragment = (MenuFragment) getSupportFragmentManager()
270                 .findFragmentById(R.id.menu_container);
271         mMenuFragment.setDrawerItemListener(mDrawerItemListener);
272         mFragmentContainerView = findViewById(R.id.menu_container);
273         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
274         setUp();
275         mMenuFragment.refresh();
276     }
277     
278     private NavigationDrawerItemListener mDrawerItemListener = new NavigationDrawerItemListener() {
279
280         public void onNavigationDrawerItemSelected(int position) {
281             if (mViewPager != null) {
282                 mViewPager.setCurrentItem(position, true);
283                 closeDrawer();
284             }
285         }
286     };
287
288     public void closeDrawer() {
289         if(mDrawerLayout != null){
290             mDrawerLayout.closeDrawer(Gravity.LEFT);
291             mDrawerOpenState=false;
292         }
293     }
294     
295     public boolean isDrawerOpen() {
296         return mDrawerLayout != null
297                 && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
298     }
299     
300     public void setUp() {
301         if (mDrawerLayout == null)
302             return;
303         mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
304                 GravityCompat.START);
305         ActionBar actionBar = getSupportActionBar();
306         actionBar.setDisplayHomeAsUpEnabled(true);
307         actionBar.setHomeButtonEnabled(true);
308
309         mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
310                 R.drawable.ic_drawer, R.string.navigation_drawer_open,
311                 R.string.navigation_drawer_close) {
312             @Override
313             public void onDrawerClosed(View drawerView) {
314                 super.onDrawerClosed(drawerView);
315                 supportInvalidateOptionsMenu();
316             }
317
318             @Override
319             public void onDrawerOpened(View drawerView) {
320                 super.onDrawerOpened(drawerView);
321                 supportInvalidateOptionsMenu(); 
322             }
323         };
324         mDrawerLayout.post(new Runnable() {
325             @Override
326             public void run() {
327                 mActionBarDrawerToggle.syncState();
328             }
329         });
330         if (mDrawerOpenState && !isDrawerOpen()) {
331             mDrawerLayout.openDrawer(Gravity.LEFT);
332         }
333         mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
334     }
335
336     public void openDrawerAnimate() {
337         new Handler().post(new Runnable() {
338
339             @Override
340             public void run() {
341                 if (mDrawerLayout != null) {
342                     mDrawerLayout.openDrawer(Gravity.LEFT);
343                     mDrawerOpenState = true;
344                 }
345             }
346         });
347
348         if (mWelcomeDialog != null) {
349             mWelcomeDialog.setOnDismissListener(null);
350         }
351     }
352 }