11
22package com .datausagecheck ;
33
4+ import android .app .AlertDialog ;
5+ import android .content .DialogInterface ;
6+ import android .os .Handler ;
7+
48import com .facebook .react .bridge .ReactApplicationContext ;
59import com .facebook .react .bridge .ReactContextBaseJavaModule ;
610import com .facebook .react .bridge .ReactMethod ;
711import com .facebook .react .bridge .Callback ;
12+ import android .app .AppOpsManager ;
13+ import android .app .usage .NetworkStats ;
14+ import android .app .usage .NetworkStatsManager ;
15+ import android .content .Context ;
16+ import android .content .Intent ;
17+ import android .content .pm .ApplicationInfo ;
18+ import android .content .pm .PackageManager ;
19+ import android .net .ConnectivityManager ;
20+ import android .os .RemoteException ;
21+ import android .provider .Settings ;
22+ import android .os .Bundle ;
23+ import android .telephony .TelephonyManager ;
24+ import java .util .Calendar ;
825
926public class RNDatausagecheckModule extends ReactContextBaseJavaModule {
1027
@@ -19,16 +36,93 @@ public RNDatausagecheckModule(ReactApplicationContext reactContext) {
1936 public String getName () {
2037 return "RNDatausagecheck" ;
2138 }
39+ private boolean isAccessGranted () {
40+ try {
41+ PackageManager packageManager = reactContext .getApplicationContext ().getPackageManager ();
42+ ApplicationInfo applicationInfo = packageManager .getApplicationInfo (reactContext .getApplicationContext ().getPackageName (), 0 );
43+ AppOpsManager appOpsManager = null ;
44+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
45+ appOpsManager = (AppOpsManager ) reactContext .getApplicationContext ().getSystemService (Context .APP_OPS_SERVICE );
46+ }
47+ int mode = 0 ;
48+ if (android .os .Build .VERSION .SDK_INT > android .os .Build .VERSION_CODES .KITKAT ) {
49+ mode = appOpsManager .checkOpNoThrow (AppOpsManager .OPSTR_GET_USAGE_STATS ,
50+ applicationInfo .uid , applicationInfo .packageName );
51+ }
52+ return (mode == AppOpsManager .MODE_ALLOWED );
53+
54+ } catch (PackageManager .NameNotFoundException e ) {
55+ return false ;
56+ }
57+
58+ }
59+
60+
61+ @ ReactMethod
62+ public void openDataUsagePermission () {
63+ Intent intent = new Intent (Settings .ACTION_USAGE_ACCESS_SETTINGS );
64+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
65+ intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
66+ intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
67+ if (intent .resolveActivity (reactContext .getPackageManager ()) != null ) {
68+ reactContext .startActivityForResult (intent ,100 ,Bundle .EMPTY );
69+ }
70+ }
71+
72+
73+
2274 @ ReactMethod
2375 public void currentTodayDataUsage (final Callback callback ) {
2476
2577 if (callback == null ) return ;
26-
2778 final Handler handler = new Handler ();
2879 handler .postDelayed (new Runnable () {
2980 @ Override
3081 public void run () {
31- callback .invoke ("You just waited on android for seconds." );
82+ if (!isAccessGranted ()) {
83+ AlertDialog .Builder builder = new AlertDialog .Builder (reactContext .getCurrentActivity ());
84+ builder .setMessage ("To access data usage you have to give permissions manually" );
85+ builder .setPositiveButton ("Open Settings" ,new DialogInterface .OnClickListener () {
86+ @ Override
87+ public void onClick (DialogInterface dialog , int which ) {
88+ openDataUsagePermission ();
89+ dialog .dismiss ();
90+ }
91+ });
92+ AlertDialog dialog = builder .create ();
93+ dialog .show ();
94+ } else {
95+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .M ) {
96+ NetworkStats .Bucket bucket = new NetworkStats .Bucket ();
97+ NetworkStatsManager networkStatsManager = (NetworkStatsManager ) reactContext .getApplicationContext ().getSystemService (Context .NETWORK_STATS_SERVICE );
98+ TelephonyManager manager = (TelephonyManager ) reactContext .getApplicationContext ().getSystemService (Context .TELEPHONY_SERVICE );
99+ String permission = "android.permission.READ_PHONE_STATE" ;
100+ int res = reactContext .checkSelfPermission (permission );
101+ if (res != PackageManager .PERMISSION_GRANTED ) {
102+ return ;
103+ }
104+ String subscriberId = manager .getSubscriberId ();
105+ Calendar calStrt = Calendar .getInstance ();
106+ calStrt .set (Calendar .MONTH ,calStrt .get (Calendar .MONTH ));
107+ calStrt .set (Calendar .DAY_OF_MONTH ,calStrt .get (Calendar .DAY_OF_MONTH ));
108+ calStrt .set (Calendar .YEAR ,calStrt .get (Calendar .YEAR ));
109+ calStrt .set (Calendar .HOUR ,0 );
110+ calStrt .set (Calendar .AM_PM ,Calendar .AM );
111+ calStrt .set (Calendar .MINUTE ,5 );
112+ calStrt .set (Calendar .SECOND ,0 );
113+ long startTime = calStrt .getTimeInMillis ();
114+ long endTime = Calendar .getInstance ().getTimeInMillis ();
115+ try {
116+ NetworkStats .Bucket dataUsage = networkStatsManager .querySummaryForDevice (ConnectivityManager .TYPE_MOBILE ,subscriberId ,startTime , endTime );
117+ long datausageInNumber = dataUsage .getRxBytes ()+dataUsage .getTxBytes ();
118+ callback .invoke ("data" +datausageInNumber );
119+ } catch (RemoteException e ) {
120+ e .printStackTrace ();
121+ }
122+ }
123+
124+ }
125+
32126 }
33127 }, 1000 );
34128 }
0 commit comments