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 ;
15
8
import android .util .Pair ;
16
- import android .view .LayoutInflater ;
17
9
import android .view .View ;
18
10
import android .widget .TextView ;
19
11
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 ;
26
12
import org .chromium .chrome .R ;
27
13
import org .chromium .chrome .browser .app .BraveActivity ;
28
14
import org .chromium .chrome .browser .brave_stats .BraveStatsBottomSheetDialogFragment ;
29
- import org .chromium .chrome .browser .local_database .DatabaseHelper ;
30
15
import org .chromium .chrome .browser .preferences .BravePref ;
31
16
import org .chromium .chrome .browser .preferences .BravePrefServiceBridge ;
32
17
import org .chromium .chrome .browser .profiles .Profile ;
33
- import org .chromium .chrome .browser .shields .BraveShieldsUtils ;
34
18
35
- import java .io .ByteArrayOutputStream ;
36
19
import java .text .SimpleDateFormat ;
37
- import java .util .ArrayList ;
38
20
import java .util .Calendar ;
39
21
import java .util .Date ;
40
- import java .util .List ;
41
22
import java .util .Locale ;
42
23
43
24
public class BraveStatsUtil {
@@ -114,6 +95,7 @@ public static String getCalculatedDate(String dateFormat, int days) {
114
95
}
115
96
116
97
public static void updateBraveStatsLayout (View view ) {
98
+ Profile mProfile = Profile .getLastUsedRegularProfile ();
117
99
TextView mAdsBlockedCountTextView =
118
100
(TextView ) view .findViewById (R .id .brave_stats_text_ads_count );
119
101
TextView mDataSavedValueTextView =
@@ -127,109 +109,24 @@ public static void updateBraveStatsLayout(View view) {
127
109
TextView mEstTimeSavedCountTextTextView =
128
110
(TextView ) view .findViewById (R .id .brave_stats_text_time_count_text );
129
111
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 ();
206
112
long trackersBlockedCount =
207
113
BravePrefServiceBridge .getInstance ().getTrackersBlockedCount (mProfile );
208
114
long adsBlockedCount = BravePrefServiceBridge .getInstance ().getAdsBlockedCount (mProfile );
209
- long adsTrackersBlockedCount = trackersBlockedCount + adsBlockedCount ;
210
115
long dataSaved = BravePrefServiceBridge .getInstance ().getDataSaved (mProfile );
211
116
long estimatedMillisecondsSaved =
212
117
(trackersBlockedCount + adsBlockedCount ) * MILLISECONDS_PER_ITEM ;
213
118
214
119
Pair <String , String > adsTrackersPair =
215
- getBraveStatsStringFormNumberPair (adsTrackersBlockedCount , false );
120
+ getBraveStatsStringFormNumberPair (adsBlockedCount , false );
216
121
Pair <String , String > dataSavedPair = getBraveStatsStringFormNumberPair (dataSaved , true );
217
122
Pair <String , String > timeSavedPair =
218
123
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 ;
232
124
233
- return getBraveStatsStringFormNumberPair (adsTrackersBlockedCount , false );
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 );
234
131
}
235
- }
132
+ }
0 commit comments