Android Open Source - SUREwalk_android Main Activity






From Project

Back to project page SUREwalk_android.

License

The source code is released under:

MIT License

If you think the Android project SUREwalk_android 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.utexas.surewalk.activities;
//w w  w  .ja va2  s  .c  om

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.crashlytics.android.Crashlytics;
import com.google.analytics.tracking.android.EasyTracker;
import com.parse.ParseAnalytics;

import org.utexas.surewalk.R;
import org.utexas.surewalk.controllers.PreferenceHandler;
import org.utexas.surewalk.fragments.DashboardFragment;

import java.util.ArrayList;

import de.keyboardsurfer.android.widget.crouton.Crouton;


public class MainActivity extends SherlockFragmentActivity {

    private PreferenceHandler mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPrefs = new PreferenceHandler(this);
        ParseAnalytics.trackAppOpened(getIntent());

        // Only enable Crashlytics if opted in and manifest key isn't our placeholder
        if (mPrefs.getCROptIn() && hasCrashlyticsApiKey(this)) {
            Crashlytics.start(this);
        }

        // Set up our views
        setContentView(R.layout.activity_main);
        getSupportActionBar().setLogo(getResources().getDrawable(R.drawable.surewalkbanner));
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        // Set up our fragments. In this case we just have one: DashboardFragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container,  DashboardFragment.newInstance("Home"));
        }

        // If it's their first time running the app, show a dialog to set up info
        if (!mPrefs.getFirstRun()) {
            mPrefs.setFirstRun();
            firstRunDialog();
        }

        // Check if keys are real or our dummies
        // may be worthwhile to comment this out during testing
        if (!mPrefs.getKeyCheck()) {
            checkKeys();
        }
    }

    private void firstRunDialog() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        alertDialogBuilder.setMessage("Looks like this is your first time using the app! Would you like to enter your information now? It speeds up the process later when you request a walk!")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // enter manually
                                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }

    private void checkKeys() {
        String maps = getResources().getString(R.string.maps_api_key);
        String parseAppKey = getResources().getString(R.string.parse_app_key);
        String parseClientKey = getResources().getString(R.string.parse_client_key);
        String analytics = getResources().getString(R.string.ga_tracking_id);
        String crashlytics = getResources().getString(R.string.crashlytics_api_key);

        ArrayList<String> missing = new ArrayList<String>();

        if (maps.equals("key_goes_here")) {
            missing.add("Maps API Key");
        }
        if (parseAppKey.equals("key_goes_here")) {
            missing.add("Parse app Key");
        }
        if (parseClientKey.equals("key_goes_here")) {
            missing.add("Parse client Key");
        }
        if (crashlytics.equals("key_goes_here")) {
            missing.add("Crashlytics Key");
        }
        if (analytics.equals("UA-xxxxxxxx-x")) {
            missing.add("Analytics tracking ID");
        }

        if (missing.size() > 0) {
            String messageKey = "It appears the following keys are missing:\n";
            for (String s : missing) {
                messageKey += "- " + s + "\n";
            }
            messageKey += "\nSome features might not work correctly.";

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("Missing keys")
                    .setMessage(messageKey)
                    .setPositiveButton("Dismiss",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();
            mPrefs.setKeyCheck(false);
        } else {
            mPrefs.setKeyCheck(true);
        }

    }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getSupportMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_set) {
         startActivity(new Intent(this, SettingsActivity.class));
         return true;
      }
      return super.onOptionsItemSelected(item);
   }

    @Override
    public void onStart() {
        super.onStart();

        // Google Analytics
        if (mPrefs.getAnalyticsOptIn()) {
            EasyTracker.getInstance(this).activityStart(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();

        // Google Analytics
        if (mPrefs.getAnalyticsOptIn()) {
            EasyTracker.getInstance(this).activityStop(this);
        }
   }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Crouton.cancelAllCroutons();
    }

    /**
     * Crashlytics' API doesn't support string replacement, so we use this to check if the API key is real or our placeholder
     * before enabling it, to prevent Gradle from crashing
     * This code is courtesy of Michael Bonnell from Crashlytics, slightly adapted
     * @return true if the Crashlytics API key's value is declared in AndroidManifest.xml metadata, otherwise return false.
     */
    private boolean hasCrashlyticsApiKey(Context context) {
        boolean hasValidKey = false;
        try {
            Context appContext = context.getApplicationContext();
            ApplicationInfo ai = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(), PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            if (bundle != null) {
                Object apiKey = bundle.get("com.crashlytics.ApiKey");
                hasValidKey = apiKey != null && apiKey instanceof String;
            }
        } catch (PackageManager.NameNotFoundException e)  {
            Log.e("CrashlyticsCheck", "Unexpected NameNotFound.", e);
        }

        return hasValidKey;
    }
}




Java Source Code List

org.utexas.surewalk.activities.AboutActivity.java
org.utexas.surewalk.activities.MainActivity.java
org.utexas.surewalk.activities.RequestWalkActivity.java
org.utexas.surewalk.activities.SettingsActivity.java
org.utexas.surewalk.classes.MainApplication.java
org.utexas.surewalk.classes.NonSwipeViewPager.java
org.utexas.surewalk.classes.OnFragmentReadyListener.java
org.utexas.surewalk.controllers.FragmentAdapter.java
org.utexas.surewalk.controllers.ParseHandler.java
org.utexas.surewalk.controllers.PreferenceHandler.java
org.utexas.surewalk.data.SureCalendar.java
org.utexas.surewalk.data.WalkRequest.java
org.utexas.surewalk.fragments.DashboardFragment.java
org.utexas.surewalk.fragments.InfoFragment.java
org.utexas.surewalk.fragments.LocationFragment.java
org.utexas.surewalk.fragments.RequestMapFragment.java
org.utexas.surewalk.fragments.ReviewFragment.java