55 */
66package ir .devage .hamrahpay ;
77
8+ import android .Manifest ;
89import android .accounts .Account ;
910import android .accounts .AccountManager ;
1011import android .annotation .SuppressLint ;
1112import android .annotation .TargetApi ;
13+ import android .app .Activity ;
1214import android .app .ProgressDialog ;
1315import android .content .Context ;
16+ import android .content .DialogInterface ;
1417import android .content .Intent ;
1518import android .content .SharedPreferences ;
19+ import android .content .pm .PackageManager ;
1620import android .graphics .Bitmap ;
1721import android .graphics .Color ;
1822import android .net .http .SslError ;
1923import android .net .wifi .WifiManager ;
2024import android .os .Build ;
2125import android .os .Bundle ;
2226import android .provider .Settings ;
27+ import android .support .v4 .app .ActivityCompat ;
28+ import android .support .v4 .content .ContextCompat ;
29+ import android .support .v7 .app .AlertDialog ;
2330import android .support .v7 .app .AppCompatActivity ;
2431import android .support .v7 .widget .Toolbar ;
2532import android .telephony .TelephonyManager ;
33+ import android .util .Log ;
2634import android .util .Patterns ;
2735import android .view .MenuItem ;
2836import android .view .View ;
3240import android .webkit .WebView ;
3341import android .webkit .WebViewClient ;
3442import android .widget .TextView ;
43+ import android .widget .Toast ;
3544
3645import com .android .volley .AuthFailureError ;
3746import com .android .volley .NetworkError ;
5867 */
5968public 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