Splash Permission - mayurparmar2/AlarmDemo GitHub Wiki
Post.java
public class SplashActivity extends AppCompatActivity {
ImageView bg;
ImageView icon;
ImageView iconFrame;
private static final int ALL_PERMISSIONS = 10;
private SharedPreferences sharedPreferences;
private static final String[] REQUIRED_PERMISSIONS = getRequiredPermissions().toArray(new String[0]);
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_splash);
getWindow().setStatusBarColor(getResources().getColor(R.color.black));
this.icon = (ImageView) findViewById(R.id.icon);
Glide.with((FragmentActivity) this).load(Integer.valueOf((int) R.drawable.sp_icon)).into(this.icon);
this.iconFrame = (ImageView) findViewById(R.id.iconFrame);
Glide.with((FragmentActivity) this).load(Integer.valueOf((int) R.drawable.sp_icon_frame)).into(this.iconFrame);
this.bg = (ImageView) findViewById(R.id.bg);
Glide.with((FragmentActivity) this).load(Integer.valueOf((int) R.drawable.sp_bg)).into(this.bg);
if (isOnline(this)) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
OpenNext1();
}
},5000);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Network error…").setMessage("Internet is not available, reconnect network and try again.").setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public final void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
finishAffinity();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
private void OpenNext1() {
new Handler().postDelayed(new Runnable() {
@Override
public final void run() {
SplashActivity.this.animateFrame();
}
}, 600L);
if (hasPermissions(this, REQUIRED_PERMISSIONS)) {
goToNext();
} else {
if (!hasPermissions(SplashActivity.this, REQUIRED_PERMISSIONS)) {
ActivityCompat.requestPermissions(SplashActivity.this, REQUIRED_PERMISSIONS, ALL_PERMISSIONS);
}else {
goToNext();
}
}
}
private boolean isFirstTime() {
return sharedPreferences.getBoolean("isFirstTime", true);
}
private static boolean hasPermissions(Context context, String... permissions) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
public void animateFrame() {
Animation loadAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
this.iconFrame.startAnimation(loadAnimation);
loadAnimation.setDuration(1000L);
loadAnimation.setRepeatMode(0);
loadAnimation.setRepeatCount(0);
}
public static boolean isOnline(Context cont) {
ConnectivityManager cm = (ConnectivityManager) cont.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == ALL_PERMISSIONS) {
boolean allGranted = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
break;
}
}
if (allGranted) {
goToNext();
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, ALL_PERMISSIONS);
}
}
}
public void goToNext(){
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
if (isFirstTime()) {
Toast.makeText(this, "Welcome to the app!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, MainActivity.class).putExtra("isFromSplash", "true"));
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isFirstTime", false);
editor.apply();
finish();
}else {
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
}