/*
* Copyright (C) Winson Chiu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cw.kop.autobackground;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
import cw.kop.autobackground.files.FileHandler;
import cw.kop.autobackground.images.FolderFragment;
import cw.kop.autobackground.history.ImageHistoryFragment;
import cw.kop.autobackground.settings.AboutFragment;
import cw.kop.autobackground.settings.AccountSettingsFragment;
import cw.kop.autobackground.settings.AppSettings;
import cw.kop.autobackground.settings.AppSettingsFragment;
import cw.kop.autobackground.settings.DownloadSettingsFragment;
import cw.kop.autobackground.settings.EffectsSettingsFragment;
import cw.kop.autobackground.settings.NotificationSettingsFragment;
import cw.kop.autobackground.settings.WallpaperSettingsFragment;
import cw.kop.autobackground.settings.WearSettingsFragment;
import cw.kop.autobackground.sources.ControllerSources;
import cw.kop.autobackground.sources.Source;
import cw.kop.autobackground.sources.SourceInfoFragment;
import cw.kop.autobackground.sources.SourceListFragment;
import cw.kop.autobackground.tutorial.TutorialActivity;
import io.fabric.sdk.android.Fabric;
public class MainActivity extends ActionBarActivity implements SourceInfoFragment.Listener,
SourceListFragment.SourceListListener {
public static final String DRAWER_OPENED = "cw.kop.autobackground.MainActivity.DRAWER_OPENED";
public static final String DRAWER_CLOSED = "cw.kop.autobackground.MainActivity.DRAWER_CLOSED";
public static final String LOAD_NAV_PICTURE = "cw.kop.autobackground.MainActivity.LOAD_NAV_PICTURE";
private static final String TAG = MainActivity.class.getCanonicalName();
private BroadcastReceiver activityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case LOAD_NAV_PICTURE:
loadNavPicture();
break;
}
}
};
private SourceListFragment sourceListFragment;
private ActionBarDrawerToggle drawerToggle;
private String[] fragmentList;
private DrawerLayout drawerLayout;
private RecyclerView recyclerNavigation;
private AdapterNavigation adapterNavigation;
private IntentFilter entryFilter;
private int currentPosition = -1;
private int newPosition = -1;
private ControllerSources controllerSources;
private LinearLayoutManager layoutManager;
public MainActivity() {
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("MP", "onConfigurationChanged");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("MP", "onCreate");
AppSettings.initPrefs(this);
switch (AppSettings.getTheme()) {
default:
case AppSettings.APP_LIGHT_THEME:
setTheme(R.style.AppLightTheme);
break;
case AppSettings.APP_DARK_THEME:
setTheme(R.style.AppDarkTheme);
break;
}
super.onCreate(savedInstanceState);
if (controllerSources == null) {
controllerSources = new ControllerSources();
}
Configuration configuration = getResources().getConfiguration();
if (configuration.screenWidthDp >= 820 || AppSettings.forceMultiPane()) {
setContentView(R.layout.activity_layout_multi_pane);
}
else {
setContentView(R.layout.activity_layout);
}
if (AppSettings.useFabric()) {
Fabric.with(this, new Crashlytics());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextAppearance(this, R.style.ToolbarTitle);
try {
setSupportActionBar(toolbar);
}
catch (Throwable t) {
}
setUpNavigationDrawer(toolbar);
if (sourceListFragment == null) {
sourceListFragment = new SourceListFragment();
}
Bundle bundle = getIntent().getExtras();
String action = getIntent().getAction();
if (bundle != null && bundle.getInt("fragment", 0) > 0) {
int position = bundle.getInt("fragment", 0);
selectItem(position, false);
}
else if (savedInstanceState == null) {
if (action != null && action.equals(Intent.ACTION_MANAGE_NETWORK_USAGE)) {
selectItem(2, false);
}
else {
selectItem(0, false);
}
}
loadNavPicture();
entryFilter = new IntentFilter();
entryFilter.addAction(MainActivity.LOAD_NAV_PICTURE);
}
private void setUpNavigationDrawer(Toolbar toolbar) {
Configuration configuration = getResources().getConfiguration();
fragmentList = getResources().getStringArray(R.array.fragment_titles);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
AdapterNavigation.NavigationClickListener navigationClickListener;
if (drawerLayout == null) {
navigationClickListener = new AdapterNavigation.NavigationClickListener() {
@Override
public void onItemClick(int position) {
selectItem(position, true);
}
@Override
public void sendIntent(Intent intent) {
sendBroadcast(intent);
}
};
}
else {
navigationClickListener = new AdapterNavigation.NavigationClickListener() {
@Override
public void onItemClick(int position) {
newPosition = position;
if (drawerLayout != null) {
drawerLayout.closeDrawer(recyclerNavigation);
}
}
@Override
public void sendIntent(Intent intent) {
sendBroadcast(intent);
}
};
}
if (adapterNavigation == null) {
adapterNavigation = new AdapterNavigation(this, fragmentList, navigationClickListener);
}
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerNavigation = (RecyclerView) findViewById(R.id.recycler_navigation);
recyclerNavigation.setHasFixedSize(true);
recyclerNavigation.setLayoutManager(layoutManager);
recyclerNavigation.setAdapter(adapterNavigation);
recyclerNavigation.getLayoutParams().width = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
Math.min(320, configuration.screenWidthDp - 56),
getResources().getDisplayMetrics()));
if (drawerLayout != null) {
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
selectItem(newPosition, true);
Intent closedIntent = new Intent(MainActivity.DRAWER_CLOSED);
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(closedIntent);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Intent openedIntent = new Intent(MainActivity.DRAWER_OPENED);
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(openedIntent);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Removes navigation animation to back arrow
super.onDrawerSlide(drawerView, 0);
}
};
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START);
}
else {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
if (AppSettings.getTheme().equals(AppSettings.APP_LIGHT_THEME)) {
recyclerNavigation.setBackgroundColor(getResources().getColor(R.color.LIGHT_THEME_BACKGROUND));
toolbar.setTitleTextColor(getResources().getColor(R.color.DARK_GRAY_OPAQUE));
}
else if (AppSettings.getTheme().equals(AppSettings.APP_DARK_THEME)) {
recyclerNavigation.setBackgroundColor(getResources().getColor(R.color.DARK_THEME_BACKGROUND));
toolbar.setTitleTextColor(getResources().getColor(R.color.LIGHT_GRAY_OPAQUE));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if (drawerToggle != null) {
drawerToggle.syncState();
}
if (AppSettings.useTutorial()) {
Intent tutorialIntent = new Intent(this, TutorialActivity.class);
tutorialIntent.putExtra(Source.POSITION, getIntent().getIntExtra(Source.POSITION, 0));
startActivityForResult(tutorialIntent, TutorialActivity.TUTORIAL_REQUEST);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TutorialActivity.TUTORIAL_REQUEST) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void onStart() {
super.onStart();Intent intent = new Intent();
intent.setAction(LiveWallpaperService.CONNECT_WEAR);
sendBroadcast(intent);
}
protected void onStop() {
super.onStop();
}
private void loadNavPicture() {
if (Build.VERSION.SDK_INT >= 16 && FileHandler.getCurrentBitmapFile() != null && FileHandler.getCurrentBitmapFile().exists()) {
adapterNavigation.loadNavPicture();
}
}
private void selectItem(int position, boolean slideAnimate) {
if (position == currentPosition || position < 0) {
return;
}
for (int num = 0; num < getFragmentManager().getBackStackEntryCount(); num++) {
getFragmentManager().popBackStack();
}
currentPosition = position;
setTitle(fragmentList[position]);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
if (slideAnimate) {
fragmentTransaction.setCustomAnimations(R.animator.slide_from_left,
R.animator.slide_to_right);
}
switch (position) {
case 0:
fragmentTransaction.replace(R.id.content_frame,
sourceListFragment,
"source_fragment").commit();
break;
case 1:
fragmentTransaction.replace(R.id.content_frame,
new WallpaperSettingsFragment()).commit();
break;
case 2:
fragmentTransaction.replace(R.id.content_frame,
new DownloadSettingsFragment()).commit();
break;
case 3:
fragmentTransaction.replace(R.id.content_frame,
new AccountSettingsFragment()).commit();
break;
case 4:
fragmentTransaction.replace(R.id.content_frame,
new EffectsSettingsFragment()).commit();
break;
case 5:
fragmentTransaction.replace(R.id.content_frame,
new NotificationSettingsFragment(),
"notification_fragment").commit();
break;
case 6:
fragmentTransaction.replace(R.id.content_frame,
new WearSettingsFragment()).commit();
break;
case 7:
fragmentTransaction.replace(R.id.content_frame, new AppSettingsFragment()).commit();
break;
case 8:
fragmentTransaction.replace(R.id.content_frame,
new ImageHistoryFragment()).commit();
break;
case 9:
fragmentTransaction.replace(R.id.content_frame, new AboutFragment()).commit();
break;
default:
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (getFragmentManager().findFragmentByTag("folder_fragment") == null) {
if (drawerToggle != null) {
return drawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
else {
getFragmentManager().popBackStack();
super.onOptionsItemSelected(item);
}
}
else if (getFragmentManager().findFragmentByTag("folder_fragment") != null) {
getFragmentManager().popBackStack();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (getFragmentManager().findFragmentByTag("folder_fragment") != null) {
((FolderFragment) getFragmentManager().findFragmentByTag("folder_fragment")).onBackPressed();
}
else if (getFragmentManager().findFragmentByTag("album_fragment") != null) {
getFragmentManager().popBackStack();
}
else if (getFragmentManager().findFragmentByTag("sourceInfoFragment") != null) {
((SourceInfoFragment) getFragmentManager().findFragmentByTag("sourceInfoFragment")).onBackPressed();
}
else if (getFragmentManager().findFragmentByTag(("notification_fragment")) != null) {
if (!((NotificationSettingsFragment) getFragmentManager().findFragmentByTag("notification_fragment")).onBackPressed()) {
super.onBackPressed();
}
}
else if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}
@Override
protected void onResume() {
super.onResume();
loadNavPicture();
LocalBroadcastManager.getInstance(this).registerReceiver(activityReceiver, entryFilter);
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(activityReceiver);
super.onPause();
}
@Override
public boolean addSource(Source source) {
boolean returnValue = controllerSources.addItem(source);
controllerSources.recount();
return returnValue;
}
@Override
public void sendToast(String message) {
if (AppSettings.useToast()) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean saveSource(Source source, int position) {
boolean returnValue = controllerSources.setItem(source, position);
controllerSources.recount();
return returnValue;
}
@Override
public ControllerSources getControllerSources() {
if (controllerSources == null) {
controllerSources = new ControllerSources();
}
return controllerSources;
}
}