public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private UserManager userManager;
private Auth0PasswordIdentityProvider passwordIdentityProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Retrieve the TapkeyServiceFactory singleton.
*/
App app = (App) getApplication();
TapkeyServiceFactory tapkeyServiceFactory = app.getTapkeyServiceFactory();
ConfigManager configManager = tapkeyServiceFactory.getConfigManager();
userManager = tapkeyServiceFactory.getUserManager();
passwordIdentityProvider = app.getPasswordIdentityProvider();
/*
* When no user is signed in, redirect to login LoginActivity
*/
if(!userManager.hasUsers()){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
return;
}
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
TextView usernameTextView = (TextView) headerView.findViewById(R.id.nav_header__username);
/*
* Get the username of first user and display it in side menu. The first user is used here,
* because we don't support multi-user yet anyways.
*/
User firstUser = userManager.getFirstUser();
if(firstUser != null){
usernameTextView.setText(firstUser.getIpUserName());
}
/*
* Verify, that app is compatible
*/
configManager.getAppState(CancellationTokens.None)
// asynchronously receive the operation result on the UI thread
.continueOnUi((Func1<ConfigManager.AppState, Void, Exception>) appState -> {
// some time might have passed and maybe the activity is already finishing.
if (isFinishing())
return null;
switch (appState) {
// SDK Version is not supported anymore
case CLIENT_TOO_OLD:
new AlertDialog.Builder(MainActivity.this)
.setMessage(R.string.main_app_too_old)
.setPositiveButton(R.string.app_too_old_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setOnDismissListener(dialogInterface -> {
// TODO: Redirect to the app store, to let the user
// download the latest app version.
finish();
})
.create()
.show();
break;
// This version of the SDK is still supported but is short before expiring and should be updated.
case CLIENT_UPDATE_SUGGESTED:
new AlertDialog.Builder(MainActivity.this)
.setMessage(R.string.newer_app_available)
.setPositiveButton(R.string.newer_app_available_button, null)
.create()
.show();
break;
case CLIENT_VERSION_OK:
default:
}
return null;
})
// Handle exceptions raised in async code.
.catchOnUi(e -> {
// Handle error
Log.e(TAG, "failed to fetch app state: ", e);
return null;
})
// Make sure, no exceptions get lost.
.conclude();
}