Android Open Source - HotSpot_Android App Start Activity






From Project

Back to project page HotSpot_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project HotSpot_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 com.ticknardif.hotspot;
/*from w w w  .  j a v  a2  s. com*/
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.ticknardif.hotspot.RESTresponses.LoginResponse;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class AppStartActivity extends Activity {
    private RestAdapter restAdapter;
    private WebService webService;
    private SharedPreferences sharedPref;

    // Create the AppStart login callback
    private Callback<LoginResponse> loginResponseCallback = new Callback<LoginResponse>() {
        @Override
        public void success(LoginResponse loginResponse, Response response) {
            if (loginResponse.success) {
                Log.d("Debug", "AppStart: Logged in with user preferences");
                Context context = getBaseContext();
                SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.shared_pref_file), Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();

                // Put the session ID in the shared preferences
                editor.putString(getString(R.string.shared_pref_session_id), loginResponse.session_id.toString());
                editor.putInt(getString(R.string.shared_pref_user_id), loginResponse.user_id);
                editor.commit();

                startMainActivity();
            }
            else {
                Log.d("Debug", "User preferences were not found or didn't work");
                startLoginActivity();
            }
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e("HTTP Error", error.toString());
            startLoginActivity();
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_start);

        TextView tv = (TextView) findViewById(R.id.fullscreen_content);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Quicksand_Book.otf");
        tv.setTypeface(tf);

        // Get the user information and last known location from SharedPreferences
        Context context = getBaseContext();
        sharedPref = context.getSharedPreferences(getString(R.string.shared_pref_file), Context.MODE_PRIVATE);
        String email = sharedPref.getString(getString(R.string.shared_pref_email), "");
        String password = sharedPref.getString(getString(R.string.shared_pref_password), "");

        // Instantiate the LocationManager and Location listener
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                Log.d("Debug", "AppStart: The User's location changed");

                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putLong("Latitude", Double.doubleToRawLongBits(location.getLatitude()));
                editor.putLong("Longitude", Double.doubleToRawLongBits(location.getLongitude()));
                editor.apply();
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
        };

        // Get the current GPS coordinates of the user
        locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, null);

        // Instantiate our REST API
        restAdapter = new RestAdapter.Builder()
                .setServer("http://54.172.35.180:8080")
                .build();
        webService = restAdapter.create(WebService.class);

        // If email and password are saved in the preferences, automatically log in
        // If we have the user email and password, try to log in
        if (email != null && password != null) {
            login(email, password);
        } else { // Else, open the LoginActivity
            startLoginActivity();
        }
    }

    // Call the API to try and log the user in
    private void login(final String email, final String password) {
        webService.login(email, password, loginResponseCallback);
    }

    //Start Main Activity with Session + Location Information
    public void startMainActivity()
    {
        Intent intent = new Intent(this, MainActivity.class);

        startActivity(intent);

        // Remove this activity from the Activity stack so the user cannot go back to this
        finish();
    }

    //Start Login Activity
    public void startLoginActivity()
    {
        Intent intent = new Intent(this, LoginActivity.class);

        startActivity(intent);

        // Remove this activity from the Activity stack so the user cannot go back to this
        finish();
    }
}




Java Source Code List

com.example.ticknardif.hotspot.ApplicationTest.java
com.example.ticknardif.hotspot.util.SystemUiHiderBase.java
com.example.ticknardif.hotspot.util.SystemUiHiderHoneycomb.java
com.example.ticknardif.hotspot.util.SystemUiHider.java
com.ticknardif.hotspot.AppStartActivity.java
com.ticknardif.hotspot.ChatroomActivity.java
com.ticknardif.hotspot.ChatroomListAdapter.java
com.ticknardif.hotspot.ChatroomOverlay.java
com.ticknardif.hotspot.Chatroom.java
com.ticknardif.hotspot.CreateAccountActivity.java
com.ticknardif.hotspot.CreateChatroomFragment.java
com.ticknardif.hotspot.GcmBroadcastReceiver.java
com.ticknardif.hotspot.GcmIntentService.java
com.ticknardif.hotspot.LoginActivity.java
com.ticknardif.hotspot.MainActivity.java
com.ticknardif.hotspot.MessageListAdapter.java
com.ticknardif.hotspot.Message.java
com.ticknardif.hotspot.WebService.java
com.ticknardif.hotspot.RESTresponses.ChatRoomCreationResponse.java
com.ticknardif.hotspot.RESTresponses.ChatroomResponse.java
com.ticknardif.hotspot.RESTresponses.ChatroomUserResponse.java
com.ticknardif.hotspot.RESTresponses.CreateChatroomResponse.java
com.ticknardif.hotspot.RESTresponses.GCMResponse.java
com.ticknardif.hotspot.RESTresponses.JoinChatroomResponse.java
com.ticknardif.hotspot.RESTresponses.LeaveChatroomResponse.java
com.ticknardif.hotspot.RESTresponses.LoginResponse.java
com.ticknardif.hotspot.RESTresponses.LogoutResponse.java
com.ticknardif.hotspot.RESTresponses.UpdateLocationResponse.java
com.ticknardif.hotspot.RESTresponses.UserResponse.java