Skip to content

Instantly share code, notes, and snippets.

@imammubin
Created January 12, 2019 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imammubin/a587192982ff8db221da14d094df6fb4 to your computer and use it in GitHub Desktop.
Save imammubin/a587192982ff8db221da14d094df6fb4 to your computer and use it in GitHub Desktop.
/*
Android Activity LAUNCHER SCREEN
+ Firebase Login Detect
+ Runable
http://github.com/imammubin
*/
package com.togebogor.www;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.togebogor.www.FeedActivity;
import com.togebogor.www.LoginActivity;
import com.togebogor.www.R;
public class MainActivity extends AppCompatActivity {
public TextView textView;
private FirebaseAuth auth;
private FirebaseUser user;
private static final String TAG = MainActivity.class.getSimpleName();
private Handler myHandler;
private Runnable myRunnableFeed,myRunnableLoginActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SharedPreferences pref = getApplicationContext().getSharedPreferences("APP_NAME", 0);
if (savedInstanceState == null) {
int success = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if (success != ConnectionResult.SUCCESS) {
GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this);
finish();
}
}
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(
MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String updatedUserToken = instanceIdResult.getToken();
Log.e("Updated FIrebaseAuth Token", updatedUserToken);
}
});
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
ConstraintLayout layer = (ConstraintLayout) findViewById(R.id.mainActivityLayer);
auth = FirebaseAuth.getInstance();
user = auth.getCurrentUser();
if(user != null) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLogin", true);
editor.apply();
}
boolean isLogin = pref.getBoolean("isLogin", false);
openActivity(isLogin);
} ///////////////////////// ENDING ONCREATE() /////////////////////////
private void openActivity(boolean isLogin) {
ConstraintLayout layer = (ConstraintLayout) findViewById(R.id.mainActivityLayer);
myHandler=new Handler();
myRunnableFeed=new Runnable() {
@Override
public void run() {
startActivity(new Intent(MainActivity.this, FeedActivity.class));
finish();
}
};
myRunnableLoginActivity=new Runnable() {
@Override
public void run() {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
};
if (isLogin) {
myHandler.postDelayed(myRunnableFeed,5000/*Delay for 5 Second*/);
layer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myHandler.removeCallbacks(myRunnableFeed);
startActivity(new Intent(MainActivity.this, FeedActivity.class));
finish();
}
});
}else{
myHandler.postDelayed(myRunnableLoginActivity,5000/* Delay for 5 Second*/);
layer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myHandler.removeCallbacks(myRunnableLoginActivity);
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment