2 Copyright (C) 2014 Samsung Electronics Polska Sp. z o.o.
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
10 http://www.gnu.org/licenses/agpl-3.0.txt
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.
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/>.
21 package com.samsung.srpol.ui;
23 import java.util.List;
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;
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;
54 public class MainActivity extends ActionBarActivity implements
55 LoaderManager.LoaderCallbacks<List<AppDetails>> {
56 public static final String TAG = "MainActivity";
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";
62 private MenuFragment mMenuFragment;
64 private WelcomeDialog mWelcomeDialog;
65 private ViewPager mViewPager;
66 private PageAdapter mFramePagerAdapter;
67 private boolean mDrawerOpenState = false;
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 setContentView(R.layout.activity_main);
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);
82 mDrawerOpenState = savedInstanceState.getBoolean(STATE_DRAWER_MENU_IS_OPEN, false);
84 showWelcomeDialog(false);
87 getSupportLoaderManager().initLoader(0, null, this);
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();
96 mFramePagerAdapter = new PageAdapter(getSupportFragmentManager(), getApplicationContext());
97 mViewPager = (ViewPager) findViewById(R.id.myviewpager);
98 mViewPager.setAdapter(mFramePagerAdapter);
100 PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.titlestrip);
101 pagerTabStrip.setDrawFullUnderline(true);
102 pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.tab_underline_color));
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());
114 if (isDrawerOpen() || (mDrawerOpenState && mDrawerLayout == null)) {
115 outState.putBoolean(STATE_DRAWER_MENU_IS_OPEN, true);
119 protected void onDestroy() {
120 if (mWelcomeDialog != null) {
121 if (mWelcomeDialog.isShowing()) {
122 mWelcomeDialog.dismiss();
128 public void restoreActionBar() {
129 ActionBar actionBar = getSupportActionBar();
130 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
131 actionBar.setDisplayShowTitleEnabled(true);
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,
141 MenuItem menuItemSystem = menu.findItem(R.id.action_toggle_system_visibility);
142 if (menuItemSystem != null) {
144 menuItemSystem.setTitle(R.string.hide_system_visibility);
146 menuItemSystem.setTitle(R.string.show_system_visibility);
149 return super.onPrepareOptionsPanel(view, menu);
153 public boolean onCreateOptionsMenu(Menu menu) {
154 getMenuInflater().inflate(R.menu.main, menu);
160 public void onBackPressed() {
161 if (isDrawerOpen()) {
164 super.onBackPressed();
169 public boolean onOptionsItemSelected(MenuItem item) {
170 if (mActionBarDrawerToggle != null
171 && mActionBarDrawerToggle.onOptionsItemSelected(item))
173 int id = item.getItemId();
175 case R.id.action_about:
176 showWelcomeDialog(true);
178 case R.id.action_licence:
181 case R.id.action_toggle_system_visibility:
182 toggleSharedPrefences(AppListLoader.PREF_INCLUDE_SYSTEM_APPS);
183 refreshAfterChanges();
186 return super.onOptionsItemSelected(item);
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();
196 public void refreshAfterChanges() {
197 supportInvalidateOptionsMenu();
198 if (mFramePagerAdapter != null) {
199 mFramePagerAdapter.refreshAdapterNotify();
200 mFramePagerAdapter.notifyDataSetChanged();
202 if (mMenuFragment != null) {
203 mMenuFragment.notifyDataSetChanged();
208 public Loader<List<AppDetails>> onCreateLoader(int id, Bundle args) {
209 AppListLoader loader = new AppListLoader(this.getApplicationContext());
210 initNavigationDrawer();
216 public void onLoadFinished(Loader<List<AppDetails>> loader,
217 List<AppDetails> data) {
218 Log.d(TAG, "onLoadFinished");
220 AppListLoader appLoader = (AppListLoader) loader;
221 if (appLoader.wasDataReloaded()) {
222 appLoader.resetWasDataReloaded();
223 mViewPager.setOffscreenPageLimit(AppListLoader.getCategories().size());
224 mFramePagerAdapter.refreshPages();
226 mFramePagerAdapter.refreshAdapterNotify();
228 if (mWelcomeDialog != null && mWelcomeDialog.isShowing()) {
229 mWelcomeDialog.loadingDone();
231 if (mMenuFragment != null) {
232 mMenuFragment.notifyDataSetChanged();
236 public void onLoaderReset(Loader<List<AppDetails>> arg0) {
237 mFramePagerAdapter.refreshAdapterNotify();
240 private void showWelcomeDialog(boolean fromMenu) {
241 Log.d(TAG, "showWelcomeActivity flag: " + fromMenu);
242 if (mWelcomeDialog == null) {
243 mWelcomeDialog = new WelcomeDialog(this, fromMenu);
245 if (!mWelcomeDialog.isShowing()) {
246 mWelcomeDialog.show(fromMenu);
249 mWelcomeDialog.setOnDismissListener(new OnDismissListener() {
252 public void onDismiss(DialogInterface dialog) {
259 private void showLicense() {
260 Intent intent = new Intent(this, LicenseActivity.class);
261 startActivity(intent);
264 private DrawerLayout mDrawerLayout;
265 private View mFragmentContainerView;
266 private ActionBarDrawerToggle mActionBarDrawerToggle;
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);
275 mMenuFragment.refresh();
278 private NavigationDrawerItemListener mDrawerItemListener = new NavigationDrawerItemListener() {
280 public void onNavigationDrawerItemSelected(int position) {
281 if (mViewPager != null) {
282 mViewPager.setCurrentItem(position, true);
288 public void closeDrawer() {
289 if(mDrawerLayout != null){
290 mDrawerLayout.closeDrawer(Gravity.LEFT);
291 mDrawerOpenState=false;
295 public boolean isDrawerOpen() {
296 return mDrawerLayout != null
297 && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
300 public void setUp() {
301 if (mDrawerLayout == null)
303 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
304 GravityCompat.START);
305 ActionBar actionBar = getSupportActionBar();
306 actionBar.setDisplayHomeAsUpEnabled(true);
307 actionBar.setHomeButtonEnabled(true);
309 mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
310 R.drawable.ic_drawer, R.string.navigation_drawer_open,
311 R.string.navigation_drawer_close) {
313 public void onDrawerClosed(View drawerView) {
314 super.onDrawerClosed(drawerView);
315 supportInvalidateOptionsMenu();
319 public void onDrawerOpened(View drawerView) {
320 super.onDrawerOpened(drawerView);
321 supportInvalidateOptionsMenu();
324 mDrawerLayout.post(new Runnable() {
327 mActionBarDrawerToggle.syncState();
330 if (mDrawerOpenState && !isDrawerOpen()) {
331 mDrawerLayout.openDrawer(Gravity.LEFT);
333 mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
336 public void openDrawerAnimate() {
337 new Handler().post(new Runnable() {
341 if (mDrawerLayout != null) {
342 mDrawerLayout.openDrawer(Gravity.LEFT);
343 mDrawerOpenState = true;
348 if (mWelcomeDialog != null) {
349 mWelcomeDialog.setOnDismissListener(null);