Skip to content

Commit 7cdf6d2

Browse files
committed
add permission request
1 parent e58244a commit 7cdf6d2

File tree

10 files changed

+95
-31
lines changed

10 files changed

+95
-31
lines changed

.idea/gradle.xml

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.2"
5+
buildToolsVersion "23.0.1"
66

77
defaultConfig {
88
applicationId "ir.devage.hamrahpay"

app/src/main/java/ir/devage/hamrahpay/sample/SampleActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void onErrorOccurred(String status, String message) {
5858
@Override
5959
public void onPaymentSucceed(String payCode) {
6060
// Save Your Payment Or Do After Payment Success
61-
Toast.makeText(MainActivity.this, "پرداخت با موفقیت انجام پذیرفت", Toast.LENGTH_SHORT).show();
61+
Toast.makeText(SampleActivity.this, "پرداخت با موفقیت انجام پذیرفت", Toast.LENGTH_SHORT).show();
6262
Log.i("HamrahPay", "payCode: " + payCode);
6363
}
6464
})

hamrahpay/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ version = '1.0.0'
66

77
android {
88
compileSdkVersion 23
9-
buildToolsVersion "23.0.2"
9+
buildToolsVersion "23.0.1"
1010

1111
defaultConfig {
1212
minSdkVersion 9

hamrahpay/src/main/java/ir/devage/hamrahpay/HamrahPay.java

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,32 @@
55
*/
66
package ir.devage.hamrahpay;
77

8+
import android.Manifest;
89
import android.accounts.Account;
910
import android.accounts.AccountManager;
1011
import android.annotation.SuppressLint;
1112
import android.annotation.TargetApi;
13+
import android.app.Activity;
1214
import android.app.ProgressDialog;
1315
import android.content.Context;
16+
import android.content.DialogInterface;
1417
import android.content.Intent;
1518
import android.content.SharedPreferences;
19+
import android.content.pm.PackageManager;
1620
import android.graphics.Bitmap;
1721
import android.graphics.Color;
1822
import android.net.http.SslError;
1923
import android.net.wifi.WifiManager;
2024
import android.os.Build;
2125
import android.os.Bundle;
2226
import android.provider.Settings;
27+
import android.support.v4.app.ActivityCompat;
28+
import android.support.v4.content.ContextCompat;
29+
import android.support.v7.app.AlertDialog;
2330
import android.support.v7.app.AppCompatActivity;
2431
import android.support.v7.widget.Toolbar;
2532
import android.telephony.TelephonyManager;
33+
import android.util.Log;
2634
import android.util.Patterns;
2735
import android.view.MenuItem;
2836
import android.view.View;
@@ -32,6 +40,7 @@
3240
import android.webkit.WebView;
3341
import android.webkit.WebViewClient;
3442
import android.widget.TextView;
43+
import android.widget.Toast;
3544

3645
import com.android.volley.AuthFailureError;
3746
import com.android.volley.NetworkError;
@@ -58,6 +67,8 @@
5867
*/
5968
public class HamrahPay {
6069

70+
public static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 100;
71+
6172
// PHP Pages On Server Side
6273
private static final String PAY_REQUEST_PAGE = "https://hamrahpay.com/rest-api/pay-request";
6374
private static final String VERIFY_PAYMENT_PAGE = "https://hamrahpay.com/rest-api/verify-payment";
@@ -287,6 +298,9 @@ protected Map<String, String> getParams() throws AuthFailureError {
287298
return params;
288299
}
289300
};
301+
if (getDeviceID(context).equals("DEVICE_ID_ERROR"))
302+
Toast.makeText(context,"لطفا سطح دسترسی لازم را به برنامه بدهید و مجددا اقدام به پرداخت نمایید.",Toast.LENGTH_LONG).show();
303+
else
290304
mRequestQueue.add(request);
291305
}
292306

@@ -359,6 +373,41 @@ protected Map<String, String> getParams() throws AuthFailureError {
359373
mRequestQueue.add(request);
360374
}
361375

376+
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
377+
public static boolean checkPermission(final Context context)
378+
{
379+
int currentAPIVersion = Build.VERSION.SDK_INT;
380+
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
381+
{
382+
383+
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
384+
385+
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_PHONE_STATE)) {
386+
387+
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
388+
alertBuilder.setCancelable(true);
389+
alertBuilder.setTitle("سطح دسترسی مورد نیاز");
390+
alertBuilder.setMessage("نیاز به شناسه ی دستگاه شما برای پرداخت");
391+
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
392+
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
393+
public void onClick(DialogInterface dialog, int which) {
394+
ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
395+
}
396+
});
397+
AlertDialog alert = alertBuilder.create();
398+
alert.show();
399+
} else {
400+
ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
401+
}
402+
return false;
403+
} else {
404+
return true;
405+
}
406+
} else {
407+
return true;
408+
}
409+
}
410+
362411
/**
363412
* Don't Forget To Set This Permissions:<br>
364413
* "android.permission.READ_PHONE_STATE"<br>
@@ -367,8 +416,13 @@ protected Map<String, String> getParams() throws AuthFailureError {
367416
* @return Device Unique ID
368417
*/
369418
private static String getDeviceID(Context context) {
370-
String deviceId;
371419

420+
421+
422+
if (checkPermission(context)) {
423+
424+
425+
String deviceId;
372426
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
373427
deviceId = telephonyManager.getDeviceId();
374428

@@ -386,6 +440,11 @@ private static String getDeviceID(Context context) {
386440
}
387441

388442
return (deviceId != null) ? deviceId : "DEVICE_ID_ERROR";
443+
}
444+
else
445+
{
446+
return "DEVICE_ID_ERROR";
447+
}
389448
}
390449

391450
/**
@@ -449,6 +508,8 @@ private static String handleVolleyError(VolleyError error) {
449508
}
450509

451510

511+
512+
452513
//==================================================================
453514
// Pay Activity As Inner Class //
454515
//==================================================================
@@ -467,6 +528,9 @@ private static void setClass(HamrahPay createdHamarahPay) {
467528
hamrahPay = createdHamarahPay;
468529
}
469530

531+
532+
Context context;
533+
470534
@Override
471535
protected void onCreate(Bundle savedInstanceState) {
472536
super.onCreate(savedInstanceState);
@@ -588,5 +652,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
588652
}
589653
return super.onOptionsItemSelected(item);
590654
}
655+
656+
657+
658+
@Override
659+
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
660+
661+
switch (requestCode) {
662+
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE:
663+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
664+
665+
} else {
666+
//code for deny
667+
}
668+
break;
669+
}
670+
671+
}
591672
}
592673
}

0 commit comments

Comments
 (0)