Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rumboalla committed Mar 14, 2017
2 parents 95d2957 + bb8993f commit 179ba40
Show file tree
Hide file tree
Showing 30 changed files with 828 additions and 542 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ captures/
# Intellij
*.iml
.idea/workspace.xml
.idea/

# Keystore files
*.jks
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:preference-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'org.jsoup:jsoup:1.9.2'
compile 'com.squareup:otto:1.3.8'
compile 'com.google.code.gson:gson:2.7'
Expand Down
191 changes: 65 additions & 126 deletions app/src/main/java/com/apkupdater/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.transition.AutoTransition;
import android.support.transition.TransitionManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.apkupdater.R;
import com.apkupdater.adapter.MainActivityPageAdapter;
import com.apkupdater.event.InstalledAppTitleChange;
import com.apkupdater.event.UpdaterTitleChange;
import com.apkupdater.fragment.LogFragment_;
import com.apkupdater.fragment.SettingsFragment;
import com.apkupdater.fragment.MainFragment;
import com.apkupdater.fragment.MainFragment_;
import com.apkupdater.fragment.SettingsFragment_;
import com.apkupdater.model.AppState;
import com.apkupdater.receiver.BootReceiver_;
import com.apkupdater.service.UpdaterService_;
import com.apkupdater.util.ColorUtitl;
import com.apkupdater.util.AnimationUtil;
import com.apkupdater.util.MyBus;
import com.apkupdater.util.ThemeUtil;
import com.squareup.otto.Subscribe;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
Expand All @@ -44,29 +42,21 @@ public class MainActivity
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@ViewById(R.id.container)
ViewPager mViewPager;

@ViewById(R.id.toolbar)
Toolbar mToolbar;

@ViewById(R.id.tabs)
TabLayout mTabLayout;

@Bean
MyBus mBus;

@Bean
AppState mAppState;

@ViewById(R.id.settings_container)
FrameLayout mSettingsLayout;

@ViewById(R.id.log_container)
FrameLayout mLogLayout;
@ViewById(R.id.container)
FrameLayout mContainer;

SettingsFragment_ mSettingsFragment;
LogFragment_ mLogFragment;
MainFragment_ mMainFragment;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -79,16 +69,6 @@ public void onCreate(
setContentView(R.layout.activity_main);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);

// Select tab
selectTab(mAppState.getSelectedTab());
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@AfterViews
Expand All @@ -97,33 +77,18 @@ void init(
checkFirstStart();
mBus.register(this);
setSupportActionBar(mToolbar);
mViewPager.setAdapter(new MainActivityPageAdapter(getBaseContext(), getSupportFragmentManager()));
mViewPager.setOffscreenPageLimit(2);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override public void onTabSelected(TabLayout.Tab tab) { mAppState.setSelectedTab(tab.getPosition()); }
@Override public void onTabUnselected(TabLayout.Tab tab) {}
@Override public void onTabReselected(TabLayout.Tab tab) {}
});
selectTab(mAppState.getSelectedTab());

if (Build.VERSION.SDK_INT <= 11) { // Fix for 2.3 background not being set properly for some reason
getWindow().getDecorView().setBackgroundColor(
ColorUtitl.getColorFromTheme(getTheme(), android.R.attr.windowBackground)
);
}

// Add the settings fragment and configure the correct state
// Create fragments
mMainFragment = new MainFragment_();
mSettingsFragment = new SettingsFragment_();
if (!(getSupportFragmentManager().findFragmentById(R.id.settings_container) instanceof SettingsFragment)) {
getSupportFragmentManager().beginTransaction().add(R.id.settings_container, mSettingsFragment).commit();
}

mLogFragment = new LogFragment_();
if (!(getSupportFragmentManager().findFragmentById(R.id.log_container) instanceof LogFragment_)) {
getSupportFragmentManager().beginTransaction().add(R.id.log_container, mLogFragment).commit();

// Add the main fragment and configure the correct state
if (!(getSupportFragmentManager().findFragmentById(R.id.container) instanceof MainFragment)) {
getSupportFragmentManager().beginTransaction().add(R.id.container, mMainFragment).commit();
}

// Switch to the correct fragment
if (mAppState.getSettingsActive()) {
switchSettings(true);
} else if (mAppState.getLogActive()) {
Expand All @@ -136,26 +101,12 @@ void init(
private void switchSettings(
boolean b
) {
ActionBar bar = getSupportActionBar();
if (b) {
mTabLayout.setVisibility(View.GONE);
mViewPager.setVisibility(View.GONE);
mSettingsLayout.setVisibility(View.VISIBLE);
mLogLayout.setVisibility(View.GONE);

if (bar != null) {
bar.setDisplayHomeAsUpEnabled(true);
bar.setTitle(getString(R.string.action_settings));
}
replaceFragment(mSettingsFragment, true);
changeToolbar(getString(R.string.action_settings), true);
} else {
mTabLayout.setVisibility(View.VISIBLE);
mViewPager.setVisibility(View.VISIBLE);
mSettingsLayout.setVisibility(View.GONE);
mLogLayout.setVisibility(View.GONE);
if (bar != null) {
bar.setTitle(getString(R.string.app_name));
bar.setDisplayHomeAsUpEnabled(false);
}
replaceFragment(mMainFragment, false);
changeToolbar(getString(R.string.app_name), false);
}

mAppState.setSettingsActive(b);
Expand All @@ -166,33 +117,59 @@ private void switchSettings(
private void switchLog(
boolean b
) {
ActionBar bar = getSupportActionBar();
if (b) {
mTabLayout.setVisibility(View.GONE);
mViewPager.setVisibility(View.GONE);
mSettingsLayout.setVisibility(View.GONE);
mLogLayout.setVisibility(View.VISIBLE);

if (bar != null) {
bar.setDisplayHomeAsUpEnabled(true);
bar.setTitle(getString(R.string.action_log));
}
replaceFragment(mLogFragment, true);
changeToolbar(getString(R.string.action_log), true);
} else {
mTabLayout.setVisibility(View.VISIBLE);
mViewPager.setVisibility(View.VISIBLE);
mSettingsLayout.setVisibility(View.GONE);
mLogLayout.setVisibility(View.GONE);
if (bar != null) {
bar.setTitle(getString(R.string.app_name));
bar.setDisplayHomeAsUpEnabled(false);
}
replaceFragment(mMainFragment, false);
changeToolbar(getString(R.string.app_name), false);
}

mAppState.setLogActive(b);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void replaceFragment(
Fragment f,
boolean in
) {
if (in) {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_right, R.anim.slide_out_left)
.replace(R.id.container, f)
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
.replace(R.id.container, f)
.commit();
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void changeToolbar(
String title,
boolean arrow
) {
try {
ActionBar bar = getSupportActionBar();
if (bar != null) {
AnimationUtil.startToolbarAnimation(mToolbar);

// This is to try to avoid the text to be cut during animation. TODO: Find a better way.
TextView t = (TextView) mToolbar.getChildAt(0);
t.getLayoutParams().width = 2000;

bar.setTitle(title);
bar.setDisplayHomeAsUpEnabled(arrow);
}
} catch (Exception e) {}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void checkFirstStart(
) {
if (mAppState.getFirstStart()) {
Expand All @@ -209,16 +186,6 @@ private void checkFirstStart(
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void selectTab(
int tab
) {
TabLayout.Tab selectedTab = mTabLayout.getTabAt(tab);
if (selectedTab != null) {
selectedTab.select();
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -266,30 +233,6 @@ protected void onDestroy() {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@Subscribe
public void onInstalledAppTitleChange(
InstalledAppTitleChange t
) {
TabLayout.Tab selectedTab = mTabLayout.getTabAt(0);
if (selectedTab != null){
selectedTab.setText(t.getTitle());
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@Subscribe
public void onUpdaterTitleChange(
UpdaterTitleChange t
) {
TabLayout.Tab selectedTab = mTabLayout.getTabAt(1);
if (selectedTab != null) {
selectedTab.setText(t.getTitle());
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void setThemeFromOptions(
) {
int theme = ThemeUtil.getActivityThemeFromOptions(getBaseContext());
Expand All @@ -305,13 +248,9 @@ protected void onResume() {

// We are checking if the theme changed
if (mAppState.getCurrentTheme() != ThemeUtil.getActivityThemeFromOptions(getBaseContext())) {
mAppState.setSelectedTab(mTabLayout.getSelectedTabPosition());
finish();
MainActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
}

// Select tab
selectTab(mAppState.getSelectedTab());
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 179ba40

Please sign in to comment.