moe.yukinoneko.gcomic.module.splash.SplashActivity.java Source code

Java tutorial

Introduction

Here is the source code for moe.yukinoneko.gcomic.module.splash.SplashActivity.java

Source

/*
 * Copyright (C) 2016  SamuelGjk <samuel.alva@outlook.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package moe.yukinoneko.gcomic.module.splash;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDelegate;

import moe.yukinoneko.gcomic.R;
import moe.yukinoneko.gcomic.base.BaseActivity;
import moe.yukinoneko.gcomic.base.IBaseView;
import moe.yukinoneko.gcomic.module.main.MainActivity;
import moe.yukinoneko.gcomic.utils.Settings;
import moe.yukinoneko.gcomic.utils.SnackbarUtils;
import moe.yukinoneko.gcomic.utils.Utils;

/**
 * Created by SamuelGjk on 2016/5/2.
 */
public class SplashActivity extends BaseActivity<SplashPresenter> implements IBaseView {

    private final int REQUEST_CODE_ASK_PERMISSIONS = 10001;

    private Handler mHandler;
    private Runnable mRunnable;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initPresenter() {
        presenter = new SplashPresenter(this, this);
        presenter.init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        boolean night = Settings.getInstance(this).getBoolean(Settings.NIGHT_MODE, false);
        getDelegate().setLocalNightMode(night ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);

        super.onCreate(savedInstanceState);
    }

    @Override
    public void init() {
        if (Utils.isConnected(this) && !Utils.isWifi(this)) {
            SnackbarUtils.showLong(findViewById(android.R.id.content), R.string.using_cellular_data);
        }

        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            new AlertDialog.Builder(this).setMessage(R.string.ask_permission_msg)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int which) {
                            ActivityCompat.requestPermissions(SplashActivity.this,
                                    new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                            Manifest.permission.READ_EXTERNAL_STORAGE },
                                    REQUEST_CODE_ASK_PERMISSIONS);
                        }
                    }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int which) {
                            showPermissionDeniedSnackbar();
                        }
                    }).show();
        } else {
            toMainActivity();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
            @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
            if (grantResults.length > 0) {
                boolean granted = true;
                for (int result : grantResults) {
                    if (result == PackageManager.PERMISSION_DENIED) {
                        showPermissionDeniedSnackbar();
                        granted = false;
                        break;
                    }
                }

                if (granted) {
                    toMainActivity();
                }
            } else {
                showPermissionDeniedSnackbar();
            }
        }
    }

    private void showPermissionDeniedSnackbar() {
        SnackbarUtils.showShort(findViewById(android.R.id.content), R.string.permission_denied);
    }

    private void toMainActivity() {
        mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        };
        mHandler.postDelayed(mRunnable, 2000);
    }

    @Override
    protected void onDestroy() {
        mHandler.removeCallbacks(mRunnable);
        super.onDestroy();
    }
}