mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-14 14:10:56 +00:00
add the Requesting Permissions at Run Time , fix [#51](https://github.com/leanote/leanote-android/issues/51)
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
package org.houxg.leamonax.ui;
|
||||
|
||||
import android.Manifest;
|
||||
import android.animation.Animator;
|
||||
import android.app.Activity;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v13.app.ActivityCompat;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.houxg.leamonax.R;
|
||||
import org.houxg.leamonax.model.Account;
|
||||
@@ -12,13 +20,56 @@ import org.houxg.leamonax.network.ApiProvider;
|
||||
import org.houxg.leamonax.service.AccountService;
|
||||
|
||||
public class LaunchActivity extends Activity {
|
||||
|
||||
final int PERMISSIONS_REQUEST_CODE=19919;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
setContentView(R.layout.activity_launch);
|
||||
if(Build.VERSION.SDK_INT >= 23){
|
||||
if(hasPermission(
|
||||
Manifest.permission.CAMERA ,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE ,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE ,
|
||||
Manifest.permission.READ_PHONE_STATE
|
||||
)){
|
||||
doAfterGetPermission();
|
||||
}else {
|
||||
AlertDialog.Builder builder=new AlertDialog.Builder(LaunchActivity.this);
|
||||
builder.setMessage(getString(R.string.permission_get_description));
|
||||
builder.setTitle(getString(R.string.permission_denied));
|
||||
builder.setPositiveButton(getString(R.string.allow), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
requestPermission(PERMISSIONS_REQUEST_CODE,
|
||||
Manifest.permission.CAMERA ,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE ,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE ,
|
||||
Manifest.permission.READ_PHONE_STATE
|
||||
);
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
builder.setCancelable(false).create().show();
|
||||
}
|
||||
}else {
|
||||
doAfterGetPermission();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
|
||||
}
|
||||
|
||||
private void doAfterGetPermission(){
|
||||
final Intent intent;
|
||||
if (AccountService.isSignedIn()) {
|
||||
Account account = Account.getCurrent();
|
||||
@@ -55,8 +106,42 @@ public class LaunchActivity extends Activity {
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
|
||||
public boolean hasPermission(String... permissions){
|
||||
for (String permission : permissions) {
|
||||
if (ActivityCompat.checkSelfPermission(this, permission)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void requestPermission(int requestCode, String... permissions) {
|
||||
ActivityCompat.requestPermissions(this, permissions, requestCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
switch (requestCode) {
|
||||
case PERMISSIONS_REQUEST_CODE:
|
||||
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
|
||||
if(hasPermission(Manifest.permission.CAMERA ,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE ,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE ,
|
||||
Manifest.permission.READ_PHONE_STATE )){
|
||||
doAfterGetPermission();
|
||||
}else {
|
||||
Toast.makeText(LaunchActivity.this,getString(R.string.permission_get_error),Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
}
|
||||
}else {
|
||||
Toast.makeText(LaunchActivity.this,getString(R.string.permission_get_error),Toast.LENGTH_SHORT).show();
|
||||
this.finish();
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -114,4 +114,8 @@
|
||||
<string name="notebook_default_root_notebook_title">空目录(最顶层目录)</string>
|
||||
<string name="delete_network_error">网络不可用,将在下次同步时删除</string>
|
||||
<string name="activity_search_note_not_found">没有找到</string>
|
||||
<string name="permission_get_error">获取权限失败,程序启动失败</string>
|
||||
<string name="permission_get_description">我们需要一些权限来启动应用,请给予我们这些应用。请放心,我们保证只会申请必要的权限,并保证绝不泄露用户隐私信息</string>
|
||||
<string name="permission_get">权限申请</string>
|
||||
<string name="allow">允许</string>
|
||||
</resources>
|
@@ -116,4 +116,8 @@
|
||||
<string name="notebook_default_root_notebook_title">Empty directory</string>
|
||||
<string name="delete_network_error">Network is unavailable, these notes will be deleted in next sync</string>
|
||||
<string name="activity_search_note_not_found">Not found</string>
|
||||
<string name="permission_get_error">Permission fetch failed, application exit</string>
|
||||
<string name="permission_get_description">We need some permission to start the application</string>
|
||||
<string name="permission_get">Permission request</string>
|
||||
<string name="allow">allow</string>
|
||||
</resources>
|
||||
|
Reference in New Issue
Block a user