5
5
6
6
package org .chromium .chrome .browser .brave_stats ;
7
7
8
+ import android .content .Context ;
9
+ import android .content .Intent ;
10
+ import android .content .pm .ResolveInfo ;
11
+ import android .graphics .Bitmap ;
12
+ import android .graphics .Canvas ;
13
+ import android .net .Uri ;
14
+ import android .provider .MediaStore ;
8
15
import android .util .Pair ;
16
+ import android .view .LayoutInflater ;
9
17
import android .view .View ;
10
18
import android .widget .TextView ;
11
19
20
+ import androidx .appcompat .app .AppCompatActivity ;
21
+
22
+ import org .chromium .base .ContextUtils ;
23
+ import org .chromium .base .Log ;
24
+ import org .chromium .base .ThreadUtils ;
25
+ import org .chromium .base .task .AsyncTask ;
12
26
import org .chromium .chrome .R ;
13
27
import org .chromium .chrome .browser .app .BraveActivity ;
14
28
import org .chromium .chrome .browser .brave_stats .BraveStatsBottomSheetDialogFragment ;
29
+ import org .chromium .chrome .browser .local_database .DatabaseHelper ;
15
30
import org .chromium .chrome .browser .preferences .BravePref ;
16
31
import org .chromium .chrome .browser .preferences .BravePrefServiceBridge ;
17
32
import org .chromium .chrome .browser .profiles .Profile ;
33
+ import org .chromium .chrome .browser .shields .BraveShieldsUtils ;
18
34
35
+ import java .io .ByteArrayOutputStream ;
19
36
import java .text .SimpleDateFormat ;
37
+ import java .util .ArrayList ;
20
38
import java .util .Calendar ;
21
39
import java .util .Date ;
40
+ import java .util .List ;
22
41
import java .util .Locale ;
23
42
24
43
public class BraveStatsUtil {
@@ -95,7 +114,6 @@ public static String getCalculatedDate(String dateFormat, int days) {
95
114
}
96
115
97
116
public static void updateBraveStatsLayout (View view ) {
98
- Profile mProfile = Profile .getLastUsedRegularProfile ();
99
117
TextView mAdsBlockedCountTextView =
100
118
(TextView ) view .findViewById (R .id .brave_stats_text_ads_count );
101
119
TextView mDataSavedValueTextView =
@@ -109,24 +127,109 @@ public static void updateBraveStatsLayout(View view) {
109
127
TextView mEstTimeSavedCountTextTextView =
110
128
(TextView ) view .findViewById (R .id .brave_stats_text_time_count_text );
111
129
130
+ List <Pair <String , String >> statsPairs = getStatsPairs ();
131
+
132
+ mAdsBlockedCountTextView .setText (statsPairs .get (0 ).first );
133
+ mDataSavedValueTextView .setText (statsPairs .get (1 ).first );
134
+ mEstTimeSavedCountTextView .setText (statsPairs .get (2 ).first );
135
+ mAdsBlockedCountTextTextView .setText (statsPairs .get (0 ).second );
136
+ mDataSavedValueTextTextView .setText (statsPairs .get (1 ).second );
137
+ mEstTimeSavedCountTextTextView .setText (statsPairs .get (2 ).second );
138
+ }
139
+
140
+ public static void updateBraveShareStatsLayoutAndShare (View view ) {
141
+ TextView mAdsBlockedCountTextView = (TextView ) view .findViewById (R .id .stats_trackers_no );
142
+ TextView mDataSavedValueTextView = (TextView ) view .findViewById (R .id .stats_saved_data_no );
143
+ TextView mEstTimeSavedCountTextView = (TextView ) view .findViewById (R .id .stats_timed_no );
144
+
145
+ List <Pair <String , String >> statsPairs = getStatsPairs ();
146
+ String trackersString =
147
+ String .format ("%s %s" , statsPairs .get (0 ).first , statsPairs .get (0 ).second );
148
+ String dataSavedString =
149
+ String .format ("%s %s" , statsPairs .get (1 ).first , statsPairs .get (1 ).second );
150
+ String timeSavedString =
151
+ String .format ("%s %s" , statsPairs .get (2 ).first , statsPairs .get (2 ).second );
152
+
153
+ mAdsBlockedCountTextView .setText (trackersString );
154
+ mDataSavedValueTextView .setText (dataSavedString );
155
+ mEstTimeSavedCountTextView .setText (timeSavedString );
156
+ shareStatsAction (view );
157
+ }
158
+
159
+ public static void shareStatsAction (View view ) {
160
+ Context context = ContextUtils .getApplicationContext ();
161
+ Bitmap bmp = convertToBitmap (view );
162
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
163
+ bmp .compress (Bitmap .CompressFormat .PNG , 100 , byteArrayOutputStream );
164
+ String path = MediaStore .Images .Media .insertImage (
165
+ context .getContentResolver (), bmp , "tempimage" , null );
166
+ Uri uri = Uri .parse (path );
167
+
168
+ Intent sendIntent = new Intent ();
169
+ sendIntent .setAction (Intent .ACTION_SEND );
170
+ sendIntent .putExtra (Intent .EXTRA_TEXT ,
171
+ context .getResources ().getString (R .string .brave_stats_share_text ));
172
+ sendIntent .putExtra (Intent .EXTRA_STREAM , uri );
173
+ sendIntent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
174
+ sendIntent .setType ("image/text" );
175
+
176
+ Intent shareIntent = Intent .createChooser (sendIntent , " " );
177
+ shareIntent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
178
+ context .startActivity (shareIntent );
179
+ }
180
+
181
+ public static View getLayout (int layoutId ) {
182
+ Context context = ContextUtils .getApplicationContext ();
183
+ LayoutInflater inflater =
184
+ (LayoutInflater ) context .getSystemService (Context .LAYOUT_INFLATER_SERVICE );
185
+ View layout = inflater .inflate (layoutId , null );
186
+
187
+ return layout ;
188
+ }
189
+
190
+ private static Bitmap convertToBitmap (View view ) {
191
+ view .measure (View .MeasureSpec .UNSPECIFIED , View .MeasureSpec .UNSPECIFIED );
192
+ int totalHeight = view .getMeasuredHeight ();
193
+ int totalWidth = view .getMeasuredWidth ();
194
+
195
+ Bitmap canvasBitmap = Bitmap .createBitmap (totalWidth , totalHeight , Bitmap .Config .ARGB_8888 );
196
+ Canvas canvas = new Canvas (canvasBitmap );
197
+ view .layout (0 , 0 , view .getMeasuredWidth (), view .getMeasuredHeight ());
198
+ view .draw (canvas );
199
+
200
+ return canvasBitmap ;
201
+ }
202
+
203
+ private static List <Pair <String , String >> getStatsPairs () {
204
+ List <Pair <String , String >> statsPair = new ArrayList <>();
205
+ Profile mProfile = Profile .getLastUsedRegularProfile ();
112
206
long trackersBlockedCount =
113
207
BravePrefServiceBridge .getInstance ().getTrackersBlockedCount (mProfile );
114
208
long adsBlockedCount = BravePrefServiceBridge .getInstance ().getAdsBlockedCount (mProfile );
209
+ long adsTrackersBlockedCount = trackersBlockedCount + adsBlockedCount ;
115
210
long dataSaved = BravePrefServiceBridge .getInstance ().getDataSaved (mProfile );
116
211
long estimatedMillisecondsSaved =
117
212
(trackersBlockedCount + adsBlockedCount ) * MILLISECONDS_PER_ITEM ;
118
213
119
214
Pair <String , String > adsTrackersPair =
120
- getBraveStatsStringFormNumberPair (adsBlockedCount , false );
215
+ getBraveStatsStringFormNumberPair (adsTrackersBlockedCount , false );
121
216
Pair <String , String > dataSavedPair = getBraveStatsStringFormNumberPair (dataSaved , true );
122
217
Pair <String , String > timeSavedPair =
123
218
getBraveStatsStringFromTime (estimatedMillisecondsSaved / 1000 );
219
+ statsPair .add (adsTrackersPair );
220
+ statsPair .add (dataSavedPair );
221
+ statsPair .add (timeSavedPair );
222
+
223
+ return statsPair ;
224
+ }
225
+
226
+ public static Pair <String , String > getAdsTrackersBlocked () {
227
+ Profile mProfile = Profile .getLastUsedRegularProfile ();
228
+ long trackersBlockedCount =
229
+ BravePrefServiceBridge .getInstance ().getTrackersBlockedCount (mProfile );
230
+ long adsBlockedCount = BravePrefServiceBridge .getInstance ().getAdsBlockedCount (mProfile );
231
+ long adsTrackersBlockedCount = trackersBlockedCount + adsBlockedCount ;
124
232
125
- mAdsBlockedCountTextView .setText (adsTrackersPair .first );
126
- mDataSavedValueTextView .setText (dataSavedPair .first );
127
- mEstTimeSavedCountTextView .setText (timeSavedPair .first );
128
- mAdsBlockedCountTextTextView .setText (adsTrackersPair .second );
129
- mDataSavedValueTextTextView .setText (dataSavedPair .second );
130
- mEstTimeSavedCountTextTextView .setText (timeSavedPair .second );
233
+ return getBraveStatsStringFormNumberPair (adsTrackersBlockedCount , false );
131
234
}
132
- }
235
+ }
0 commit comments