Back to project page android.app.niuz.io.
The source code is released under:
GNU General Public License
If you think the Android project android.app.niuz.io listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package io.niuz; //from w w w. j a va 2 s .c o m import io.niuz.model.APIRetrievalStatus; import io.niuz.model.InitUserStatus; import io.niuz.services.CategoriesSubcategoriesService; import io.niuz.services.UserService; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private final String textInitializingPhone = "Initializing phone..."; private final String textInitializingCategories = "Syncing categories..."; private final String textInitializingSubcategories = "Syncing subcategories..."; private final String failedInitializingUser = "Failed to initialize new user!"; private final String failedInitializingCategories = "Failed to download categories!"; private final String failedInitializingSubcategories = "Failed to download subcategories!"; private final String welcomeBack = "Welcome back..."; private final String syncComplete = "Syncing has been completed!"; private TextView loadingText; private ProgressBar progressBar; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.context = this; // Setting up the text this.loadingText = (TextView)findViewById(R.id.maLoadingText); this.loadingText.setText(this.textInitializingPhone); // Setting up the loader this.progressBar = (ProgressBar)findViewById(R.id.maProgressBar); this.progressBar.setVisibility(View.VISIBLE); // Check if user exists and inform in UI this.checkUserAndInform(); } private void checkUserAndInform() { new InitUserTask().execute(); } private void transferToInitCategoriesActivity() { startActivity(new Intent(this, CategoriesSelectionActivity.class)); } private void transferToNewsHeadersActivity() { startActivity(new Intent(this, NewsHeadersActivity.class)); } private class RetrieveSubcategoriesTask extends AsyncTask<String, Integer, APIRetrievalStatus> { @Override protected APIRetrievalStatus doInBackground(String... params) { try { CategoriesSubcategoriesService.initSubcategories(); return APIRetrievalStatus.SUCCESS; } catch (Exception e) { e.printStackTrace(); Log.d("error", e.getMessage()); return APIRetrievalStatus.ERROR; } } @Override protected void onPostExecute(APIRetrievalStatus result) { switch (result) { case SUCCESS: loadingText.setText(syncComplete); transferToInitCategoriesActivity(); break; case ERROR: progressBar.setVisibility(View.GONE); loadingText.setText(failedInitializingSubcategories); } } } private class RetrieveCategoriesTask extends AsyncTask<String, Integer, APIRetrievalStatus> { @Override protected APIRetrievalStatus doInBackground(String... arg0) { try { CategoriesSubcategoriesService.initCategories(); return APIRetrievalStatus.SUCCESS; } catch (Exception e) { e.printStackTrace(); Log.d("error", e.getMessage()); return APIRetrievalStatus.ERROR; } } @Override protected void onPostExecute(APIRetrievalStatus result) { switch (result) { case SUCCESS: loadingText.setText(textInitializingSubcategories); new RetrieveSubcategoriesTask().execute(); break; case ERROR: progressBar.setVisibility(View.GONE); loadingText.setText(failedInitializingCategories); } } } private class InitUserTask extends AsyncTask<Object, Integer, InitUserStatus> { @Override protected InitUserStatus doInBackground(Object... params) { try { return UserService.initUser(context) ? InitUserStatus.NEW : InitUserStatus.OLD; } catch (Exception e) { e.printStackTrace(); Log.d("error", e.getMessage()); return InitUserStatus.ERROR; } } @Override protected void onPostExecute(InitUserStatus result) { switch (result) { case NEW: loadingText.setText(textInitializingCategories); new RetrieveCategoriesTask().execute(); break; case OLD: loadingText.setText(welcomeBack); transferToNewsHeadersActivity(); break; case ERROR: progressBar.setVisibility(View.GONE); loadingText.setText(failedInitializingUser); } } } @Override public void onBackPressed() { startActivity(new Intent(context, PreMainActivity.class)); } }