Skip to content

Commit

Permalink
Add EasyPermissionsActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
0xm1nam0 committed Dec 10, 2018
1 parent f4ea6ba commit 3f1b5c3
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 50 deletions.
28 changes: 14 additions & 14 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'me.tatarka.retrolambda'
//apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
compileSdkVersion 28
defaultConfig {
applicationId "com.github.weiss.example"
minSdkVersion 15
targetSdkVersion 27
targetSdkVersion 28
versionCode 2
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -42,17 +41,18 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
compile "com.jakewharton:butterknife:$rootProject.ext.butterknifeVersion"
implementation 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
implementation "com.jakewharton:butterknife:$rootProject.ext.butterknifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.ext.butterknifeVersion"
compile 'com.tencent.bugly:crashreport:2.4.0'
testCompile 'junit:junit:4.12'
compile 'in.srain.cube:ultra-ptr:1.0.11'
compile "com.android.support:cardview-v7:$rootProject.ext.androidSupportSdkVersion"
compile 'com.github.chrisbanes.photoview:library:1.2.3'
compile project(':core')
implementation 'com.tencent.bugly:crashreport:2.4.0'
testImplementation 'junit:junit:4.12'
implementation 'in.srain.cube:ultra-ptr:1.0.11'
implementation "com.android.support:cardview-v7:$rootProject.ext.androidSupportSdkVersion"
implementation 'com.github.chrisbanes.photoview:library:1.2.3'
implementation 'pub.devrel:easypermissions:2.0.0'
implementation project(':core')
}
11 changes: 9 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
Expand All @@ -17,15 +19,20 @@
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name="com.github.weiss.example.ui.MainActivity"
android:name="com.github.weiss.example.ui.SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
android:theme="@style/StartTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.github.weiss.example.ui.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
</activity>

<activity
android:name="com.github.weiss.example.ui.PictureActivity" />
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/github/weiss/example/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.weiss.example;

import com.github.weiss.core.BaseApp;
import com.github.weiss.core.UserManager;
import com.github.weiss.example.entity.UserModel;
import com.squareup.leakcanary.LeakCanary;

import butterknife.ButterKnife;
Expand All @@ -10,11 +12,15 @@
*/

public class App extends BaseApp {

public static UserManager<UserModel> userManager;

@Override
public void onCreate() {
super.onCreate();
ButterKnife.setDebug(true);
// CrashReport.initCrashReport(getApplicationContext(), "", false);
userManager = new UserManager<>(UserModel.class);
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/github/weiss/example/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.weiss.core.BaseRxActivity;
import com.github.weiss.core.entity.BaseHttpResult;
import com.github.weiss.core.utils.LogUtils;
import com.github.weiss.core.utils.ToastUtils;

/**
* Created by Weiss on 2017/1/17.
Expand All @@ -11,6 +13,9 @@ public abstract class BaseActivity extends BaseRxActivity {

//token失效处理
public void tokenInvalid() {
LogUtils.d("tokenInvalid");
App.userManager.logout();
ToastUtils.show("请安全退出,重新登录账号");

}

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/github/weiss/example/entity/UserModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.weiss.example.entity;

/**
* author weiss
* email [email protected]
* created 2018/1/30.
*/
public class UserModel {

public int id;

public UserModel(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.github.weiss.example.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.github.weiss.example.BaseActivity;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public abstract class EasyPermissionsActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks,
EasyPermissions.RationaleCallbacks {

private static final String TAG = "EasyPermissionsActivity";
private static final String[] PERMISSION = {
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA
};

private static final int RC_PERM = 124;
private boolean isFirst = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isFirst = true;
}

@Override
protected void onResume() {
super.onResume();
if (isFirst) {
//因为要通过一个Fragment来弹出弹出框,所以activity这里的onResume执行了两次,这里进行判断
isFirst = false;
permissionsTask();

}
}

private boolean hasPermissions() {
return EasyPermissions.hasPermissions(this, PERMISSION);
}

private boolean hasStoragePermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}


@AfterPermissionGranted(RC_PERM)
public void permissionsTask() {
if (hasPermissions()) {
startMainActivity();
// Have permissions, do the thing!
} else {
// Ask for both permissions
EasyPermissions.requestPermissions(
this,
"需要获取以下权限,才能正常使用APP",
RC_PERM,
PERMISSION);
}
}

public abstract void startMainActivity();

@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

// EasyPermissions handles the request result.
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

//接受的权限List
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());
}

//拒绝的权限List
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
// This will display a dialog directing them to enable the permission in app settings.
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
//这个方法有个前提是,用户点击了“不再询问”后,才判断权限没有被获取到
new AppSettingsDialog.Builder(this)
.setRationale("没有该权限,此应用程序可能无法正常工作。打开应用设置界面以修改应用权限")
.setTitle("必需权限")
.build()
.show();
isFirst = true;
} else if (!hasPermissions()) {
//这里响应的是除了AppSettingsDialog这个弹出框,剩下的两个弹出框被拒绝或者取消的效果
finish();
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
if (!hasPermissions()) {
finish();
}
}
}

@Override
public void onRationaleAccepted(int requestCode) {
Log.d(TAG, "onRationaleAccepted:" + requestCode);
}

@Override
public void onRationaleDenied(int requestCode) {
Log.d(TAG, "onRationaleDenied:" + requestCode);
}
}
57 changes: 57 additions & 0 deletions app/src/main/java/com/github/weiss/example/ui/SplashActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.weiss.example.ui;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.flyco.systembar.SystemBarHelper;
import com.github.weiss.example.BaseActivity;
import com.github.weiss.example.R;

/**
* author weiss
* email [email protected]
* created 2017/12/15.
*/
public class SplashActivity extends EasyPermissionsActivity {

@Override
protected int getLayoutId() {
//隐藏标题栏以及状态栏
/* getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
*//**标题是属于View的,所以窗口所有的修饰部分被隐藏后标题依然有效,需要去掉标题**//*
requestWindowFeature(Window.FEATURE_NO_TITLE);*/
return R.layout.activity_splash;
}

@Override
protected void initView() {
SystemBarHelper.immersiveStatusBar(this);
}

@Override
protected void onDestroy() {
handler.removeMessages(0);
super.onDestroy();
}

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
getHome();
super.handleMessage(msg);
}
};

public void getHome() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

@Override
public void startMainActivity() {
handler.sendEmptyMessageDelayed(0, 1500);
}
}
30 changes: 30 additions & 0 deletions app/src/main/res/layout/activity_splash.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<uk.co.senab.photoview.PhotoView
android:id="@+id/picture"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>-->

</android.support.design.widget.CoordinatorLayout>
Binary file added app/src/main/res/mipmap-xxhdpi/bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="StartTheme" parent="AppTheme">
<item name="android:windowBackground">@mipmap/bg</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Expand Down
Loading

0 comments on commit 3f1b5c3

Please sign in to comment.