Skip to content

Commit

Permalink
Add permission check for Android 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin20150405 committed May 25, 2017
1 parent b019787 commit 4c33c50
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 24 deletions.
19 changes: 3 additions & 16 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.2.0'
compile project(':OpenCVLibrary320')
}
13 changes: 8 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation">
android:supportsRtl="true">
<activity android:name=".HomeActivity"
android:theme="@style/AppThemeCompat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
</application>

</manifest>
3 changes: 1 addition & 2 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <jni.h>
#include <string>
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#include <android/log.h>
Expand Down Expand Up @@ -33,7 +32,7 @@ void Java_com_martin_ads_testopencv_MainActivity_nativeProcessFrame(JNIEnv*, job
detector->detect(mGr, v);
for (unsigned int i = 0; i < v.size(); i++) {
const KeyPoint &kp = v[i];
circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255, 0, 0, 255));
circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255, 255, 255, 255));
}


Expand Down
70 changes: 70 additions & 0 deletions app/src/main/java/com/martin/ads/testopencv/HomeActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.martin.ads.testopencv;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;


public class HomeActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION = 233;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(checkPermission(Manifest.permission.CAMERA,REQUEST_PERMISSION))
init();
}

private void init(){
startActivity(new Intent(this,MainActivity.class));
finish();
}

private boolean checkPermission(String permission,int requestCode){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
showHint("Camera and SDCard access is required, please grant the permission in settings.");
finish();
} else {
ActivityCompat.requestPermissions(this,
new String[]{
permission,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
requestCode);
}
return false;
}else return true;
}
return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
init();
}else {
showHint("Camera and SDCard access is required, please grant the permission in settings.");
finish();
}
break;
default:
finish();
break;
}
}

private void showHint(String hint){
Toast.makeText(this,hint , Toast.LENGTH_LONG).show();
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
backward-compatibility can go here.
-->
</style>

<style name="AppThemeCompat" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 4c33c50

Please sign in to comment.