Android Open Source - unicef_gis_mobile My Reports Activity






From Project

Back to project page unicef_gis_mobile.

License

The source code is released under:

MIT License

If you think the Android project unicef_gis_mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.unicef.gis.ui;
/*from   w ww. j av a  2 s  .  com*/
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.unicef.gis.R;
import org.unicef.gis.auth.Authenticator;
import org.unicef.gis.infrastructure.RoutesResolver;
import org.unicef.gis.infrastructure.ServerUrlPreferenceNotSetException;
import org.unicef.gis.infrastructure.data.UnicefGisStore;
import org.unicef.gis.model.Report;
import org.unicef.gis.model.couchdb.ReportLoader;
import org.unicef.gis.ui.report.CreateReportActivity;
import org.unicef.gis.ui.report.ReportRowAdapter;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.couchbase.cblite.router.CBLURLStreamHandlerFactory;

public class MyReportsActivity extends ListActivity implements LoaderCallbacks<List<Report>>, AccountManagerCallback<Bundle> {  
  {
    CBLURLStreamHandlerFactory.registerSelfIgnoreError();
    }
  
  private static final int LOADER_ID = 111; 
  
  private TextView emptyView;
  private Button newReportButton;
  private ReportRowAdapter dbAdapter;
  
  //There seems to be a bug in the way loaders interact with the activity lifecycle, 
  //which forces us to call loaders onCreate, so we use this flag in order not to
  //restart loaders twice when we come back to this activity from another one without
  //recreating it.
  private boolean justCreated = false;
      
  private AccountManager accountManager = null;

  private Timer timer;
  
  private ProgressDialog deleteReportsProgressDialog = null;
      
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    
    setContentView(R.layout.activity_my_reports);
    
    accountManager = AccountManager.get(this);
    
    justCreated = true;
            
    loadControls();
    
    emptyView.setText(R.string.no_reports);
    newReportButton.setText(R.string.new_report);  
    
    if (checkAddressPreference() && checkTags()){      
      setupAdapter();                    
      refreshData();                  
    }
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.delete_uploaded) {
      showDeleteReportsDialog();
    } else {
      openPreferences();
    }
    
    return true;
  }
  
  private void showDeleteReportsDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(R.string.delete_uploaded_message)
           .setTitle(R.string.delete_uploaded);
    
    builder.setPositiveButton(R.string.delete_uploaded_confirm, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
        showDeleteReportsProgressDialog();
        
      }
    });
    
    builder.setNegativeButton(R.string.delete_uploaded_cancel, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {}
    });
    
    AlertDialog dialog = builder.create();
    dialog.show();
  }
  
  private void showDeleteReportsProgressDialog() {  
    deleteReportsProgressDialog = new ProgressDialog(this);
    
    deleteReportsProgressDialog.setTitle(R.string.deleting_reports);
    deleteReportsProgressDialog.setMessage(getResources().getString(R.string.please_wait_deleting_reports));
    deleteReportsProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    
    deleteReportsProgressDialog.setProgress(0);
    deleteReportsProgressDialog.setMax(20);    
    
    deleteReportsProgressDialog.show();
    
    DeleteUploadedReportsTask task = new DeleteUploadedReportsTask(this);
    task.execute();
  }

  private void openPreferences() {
    startActivity(new Intent(this, SettingsActivity.class));
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_reports_actions, menu);
    
    return super.onCreateOptionsMenu(menu);
  }

  private void scheduleSync(Account account) {  
    ContentResolver.setSyncAutomatically(account, "org.unicef.gis.provider", true);
    
        ContentResolver.addPeriodicSync(
                account,
                "org.unicef.gis.provider",
                new Bundle(),
                5);
  }

  private void setupAccount() {
    Account[] accounts = accountManager.getAccountsByType(Authenticator.ACCOUNT_TYPE);
    
    if (accounts.length == 0) {
      Bundle options = new Bundle();
      options.putBoolean(AuthenticatorActivity.PARAM_SHOULD_AUTHENTICATE, false);
      
      accountManager.addAccount(Authenticator.ACCOUNT_TYPE, Authenticator.AUTH_TOKEN_TYPE, null, options, this, this, null);
    } else {      
      scheduleSync(accounts[0]);
    }    
  }

  private void refreshData() {
    displaySpinningWheelWhileLoading();
    dbAdapter.notifyDataSetChanged();
    getListView().invalidateViews();
    
    LoaderManager lm = getLoaderManager();
    if (lm.getLoader(LOADER_ID) != null) {          
      lm.restartLoader(LOADER_ID, null, this);
    } else {
      lm.initLoader(LOADER_ID, null, this);          
    }
  }
  
  @Override
  protected void onResume() {
    setupAccount();
    
    if (!justCreated)
      refreshData();      

    justCreated = false;
    
    setupRefreshTimer();
    
    super.onResume();
  }
  
  private void setupRefreshTimer() {
    timer = new Timer();
    timer.schedule(new TimerTask() {      
      @Override
      public void run() {
        runOnUiThread(new Runnable() {          
          @Override
          public void run() {
            refreshData();            
          }
        });
      }
    }, 30000, 30000);
  }

  @Override
  protected void onPause() {
    super.onPause();
    
    cancelRefreshTimer();
  }
  
  private void cancelRefreshTimer() {
    if (timer != null)
      timer.cancel();
    
    timer = null;
  }

  public void startCreateReportActivity(View view) {
    startActivity(new Intent(this, CreateReportActivity.class));
  }
  
  private void setupAdapter() {
    dbAdapter = new ReportRowAdapter(this, R.layout.row_report);
    setListAdapter(dbAdapter);        
  }

  private void displaySpinningWheelWhileLoading() {
    setProgressBarIndeterminate(true);
    setProgressBarIndeterminateVisibility(true);
  }

  private void loadControls() {
    newReportButton = (Button) findViewById(R.id.my_reports_new_report);
    emptyView = (TextView) findViewById(R.id.my_reports_empty_message);    
  }

  private boolean checkTags() {
    UnicefGisStore store = new UnicefGisStore(this);
    
    if (!store.tagsHaveBeenFetched()) {
      startActivity(new Intent(this, FetchTagsActivity.class));
      return false;
    }  
    
    return true;
  }

  private boolean checkAddressPreference() {
    RoutesResolver routes = new RoutesResolver(this);
    try {
      routes.getBaseUrl();
      return true;
    } catch (ServerUrlPreferenceNotSetException e) {
      startActivity(new Intent(this, ConfigureServerUrlActivity.class));
      return false;
    }   
  }

  @Override
  public Loader<List<Report>> onCreateLoader(int id, Bundle args) {
        return new ReportLoader(getApplicationContext());
  }

  @Override
  public void onLoaderReset(Loader<List<Report>> loader) {
    dbAdapter.clear();
  }

  @Override
  public void onLoadFinished(Loader<List<Report>> loader, List<Report> reports) {
    dbAdapter.clear();
    dbAdapter.addAll(reports);
    
    setProgressBarIndeterminateVisibility(false);
  }

  @Override
  public void run(AccountManagerFuture<Bundle> arg0) {
    setupAccount();
  }

  public void reportProgress() {
    if (deleteReportsProgressDialog != null) {
      deleteReportsProgressDialog.incrementProgressBy(1);
    }
  }

  public void informReportsToDelete(Integer totalReportsToDelete) {
    if (deleteReportsProgressDialog != null) {
      deleteReportsProgressDialog.setMax(totalReportsToDelete);
    }
  }

  public void reportsDeleted() {
    if (deleteReportsProgressDialog != null) {
      deleteReportsProgressDialog.dismiss();
      deleteReportsProgressDialog = null;
    }
    
    refreshData();
  }
}




Java Source Code List

com.couchbase.cblite.ektorp.CBLiteHttpClient.java
com.couchbase.cblite.ektorp.CBLiteHttpResponse.java
edu.mit.mobile.android.utils.StreamUtils.java
org.unicef.gis.auth.AuthenticatorService.java
org.unicef.gis.auth.Authenticator.java
org.unicef.gis.infrastructure.CompileTimeSettings.java
org.unicef.gis.infrastructure.ILocationServiceConsumer.java
org.unicef.gis.infrastructure.LocationService.java
org.unicef.gis.infrastructure.Network.java
org.unicef.gis.infrastructure.Notificator.java
org.unicef.gis.infrastructure.RoutesResolver.java
org.unicef.gis.infrastructure.ServerUrlPreferenceNotSetException.java
org.unicef.gis.infrastructure.UnicefGisApi.java
org.unicef.gis.infrastructure.data.CouchDbLiteStoreAdapter.java
org.unicef.gis.infrastructure.data.UnicefGisContentProvider.java
org.unicef.gis.infrastructure.data.UnicefGisStore.java
org.unicef.gis.infrastructure.image.AsyncDrawable.java
org.unicef.gis.infrastructure.image.BitmapWorkerTask.java
org.unicef.gis.infrastructure.image.Camera.java
org.unicef.gis.model.Report.java
org.unicef.gis.model.Tag.java
org.unicef.gis.model.couchdb.NullReduce.java
org.unicef.gis.model.couchdb.ReportLoader.java
org.unicef.gis.model.couchdb.views.AllReportsByTimestampDesc.java
org.unicef.gis.model.couchdb.views.PendingSyncReports.java
org.unicef.gis.model.couchdb.views.UnicefGisView.java
org.unicef.gis.model.couchdb.views.UploadedReports.java
org.unicef.gis.sync.SyncAdapter.java
org.unicef.gis.sync.SyncService.java
org.unicef.gis.ui.AlertDialogFragment.java
org.unicef.gis.ui.AuthenticatorActivity.java
org.unicef.gis.ui.ConfigureServerUrlActivity.java
org.unicef.gis.ui.DeleteUploadedReportsTask.java
org.unicef.gis.ui.FetchTagsActivity.java
org.unicef.gis.ui.FetchTagsTask.java
org.unicef.gis.ui.MyReportsActivity.java
org.unicef.gis.ui.SettingsActivity.java
org.unicef.gis.ui.SettingsFragment.java
org.unicef.gis.ui.report.ChooseTagsFragment.java
org.unicef.gis.ui.report.CreateReportActivityConstants.java
org.unicef.gis.ui.report.CreateReportActivity.java
org.unicef.gis.ui.report.GetTagsTaskFragment.java
org.unicef.gis.ui.report.GetTagsTask.java
org.unicef.gis.ui.report.IChooseTagsCallbacks.java
org.unicef.gis.ui.report.IGetTagsCallback.java
org.unicef.gis.ui.report.IGetTagsTaskFragmentCallbacks.java
org.unicef.gis.ui.report.IReportSummaryCallbacks.java
org.unicef.gis.ui.report.ReportRowAdapter.java
org.unicef.gis.ui.report.ReportSummaryFragment.java
org.unicef.gis.ui.report.ReportViewModel.java
org.unicef.gis.ui.report.ToggleTagAdapter.java