Skip to content

Commit

Permalink
Merge pull request #20 from cedricdenis/correctPermissions
Browse files Browse the repository at this point in the history
Correct requesting permissions for Android >= M
  • Loading branch information
gillesdubois authored Sep 25, 2017
2 parents cea55e0 + 4527d13 commit 6025de8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 59 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
android:versionCode="230"
android:versionName="2.3.0RC">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
*/
package org.ocs.android.agent.activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
Expand All @@ -40,19 +40,19 @@
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.ocs.android.actions.OCSProtocol;
import org.ocs.android.actions.OCSSettings;
import org.ocs.android.actions.PrefsParser;
import org.ocs.android.actions.Utils;
import org.ocs.android.agent.AboutDialog;
import org.ocs.android.agent.AsyncOperations;
import org.ocs.android.agent.R;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
Expand All @@ -63,24 +63,13 @@ public class OCSAgentActivity extends AppCompatActivity implements ActivityCompa
private final static String IMPORT_CONFIG = "import_config";
protected ProgressDialog mProgressDialog;

//ID to identify a camera permission request.
private static final int PERMISSIONS_REQUEST_CAMERA = 1;
private static final int PERMISSIONS_REQUEST_EXTERNAL_STORAGE= 2;
private static final int PERMISSIONS_REQUEST_PHONE_STATE= 3;

private static final int REQUEST_PERMISSION_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ocs_agent);

String[] permissionName = {Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE};
int[] permissionRequest = {PERMISSIONS_REQUEST_CAMERA,PERMISSIONS_REQUEST_EXTERNAL_STORAGE,PERMISSIONS_REQUEST_PHONE_STATE};

for(int i=0; i<permissionName.length; i++){
this.requestPermissions(permissionName[i], permissionRequest[i]);
}

// Initialize configuration.
// If an extra "IMPORT_CONFIG" is added on launch of the activity, the configuration will be imported
settings = OCSSettings.getInstance(this);
Expand All @@ -107,7 +96,7 @@ protected void onCreate(Bundle savedInstanceState) {
new OCSProtocol(getApplicationContext()).verifyNewVersion(vcode);


/**
/*
* Actions for buttons
*/
// Send Inventory
Expand Down Expand Up @@ -136,45 +125,38 @@ public void onClick(View v) {
spawnTask(false);
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
// Check permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] ocsPermissions = new String[]{
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.CAMERA,
android.Manifest.permission.READ_PHONE_STATE};

List<String> permissionNeeded = new ArrayList<>();
for (String permission:ocsPermissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
permissionNeeded.add(permission);
}
return;
}
case PERMISSIONS_REQUEST_EXTERNAL_STORAGE:{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
if (!permissionNeeded.isEmpty()) {
requestPermissions(permissionNeeded.toArray(new String[permissionNeeded.size()]), REQUEST_PERMISSION_CODE);
}
case PERMISSIONS_REQUEST_PHONE_STATE:{
// If request is cancelled, the result arrays are empty.
}
}

@Override
public void onRequestPermissionsResult(
int requestCode,
String permissions[],
int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSION_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
&& grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(this, getResources().getString(R.string.must_accept_permissions) , Toast.LENGTH_SHORT).show();
}
return;
}
break;
}
}

Expand Down Expand Up @@ -278,15 +260,7 @@ private void setStatus(String msg) {
status.setText(msg);
}

private void requestPermissions(String permissionName, int permissionRequest){
//request permission.
if (ContextCompat.checkSelfPermission(OCSAgentActivity.this, permissionName)
!= PackageManager.PERMISSION_GRANTED) {

// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(OCSAgentActivity.this,
new String[]{permissionName},
permissionRequest);
}
private void checkAndRequestPermissions() {
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
<string name="pref_title_compua">Useragent Vereinbarkeit</string>
<string name="title_activity_ocslaunch">Installation</string>
<string name="start_download_service">Starten des Update-Service</string>
<string name="must_accept_permissions">Sie müssen alle Berechtigungen akzeptieren</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
<string name="pref_description_syshide">Ne pas inclure les logiciels systèmes</string>
<string name="title_activity_ocslaunch">Installation</string>
<string name="start_download_service">Lancement du service d\'update</string>
<string name="must_accept_permissions">Vous devez accepter toutes les permissions</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@
<string name="title_activity_ocslaunch">Installation</string>
<string name="err_cant_connect">Unable to connect to : </string>
<string name="start_download_service">Start package update service</string>
<string name="must_accept_permissions">You must accept all permissions</string>

</resources>

0 comments on commit 6025de8

Please sign in to comment.