package se.leap.bitmaskclient; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import se.leap.bitmaskclient.drawer.NavigationDrawerFragment; import se.leap.bitmaskclient.eip.EipCommand; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; public class MainActivity extends AppCompatActivity { private static Provider provider = new Provider(); private static FragmentManagerEnhanced fragmentManager; private SharedPreferences preferences; private NavigationDrawerFragment navigationDrawerFragment; public final static String ACTION_SHOW_VPN_FRAGMENT = "action_show_vpn_fragment"; /** * Fragment managing the behaviors, interactions and presentation of the navigation drawer. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); provider = ConfigHelper.getSavedProviderFromSharedPreferences(preferences); fragmentManager = new FragmentManagerEnhanced(getSupportFragmentManager()); // Set up the drawer. navigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); handleIntentAction(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); handleIntentAction(intent); } private void handleIntentAction(Intent intent) { if (intent == null || intent.getAction() == null) { return; } switch (intent.getAction()) { case ACTION_SHOW_VPN_FRAGMENT: showEipFragment(); break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data == null) { return; } if (resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { provider = data.getParcelableExtra(Provider.KEY); if (provider == null) { return; } ConfigHelper.storeProviderInPreferences(preferences, provider); navigationDrawerFragment.refresh(); switch (requestCode) { case REQUEST_CODE_SWITCH_PROVIDER: EipCommand.stopVPN(this); break; case REQUEST_CODE_CONFIGURE_LEAP: break; case REQUEST_CODE_LOG_IN: EipCommand.startVPN(this); break; } showEipFragment(); } } private void showEipFragment() { Fragment fragment = new EipFragment(); Bundle arguments = new Bundle(); arguments.putParcelable(PROVIDER_KEY, provider); fragment.setArguments(arguments); fragmentManager.beginTransaction() .replace(R.id.container, fragment) .commit(); } }