1717 */
1818package au .com .wallaceit .reddinator .activity ;
1919
20+ import android .annotation .SuppressLint ;
2021import android .annotation .TargetApi ;
2122import android .app .ActionBar ;
23+ import android .app .AlertDialog ;
24+ import android .app .ProgressDialog ;
2225import android .content .BroadcastReceiver ;
23- import android .content .Context ;
2426import android .content .Intent ;
25- import android .content .IntentFilter ;
26- import android .content .SharedPreferences ;
2727import android .content .res .Resources ;
2828import android .graphics .Color ;
29+ import android .os .AsyncTask ;
2930import android .os .Build ;
3031import android .os .Bundle ;
31- import android .preference .PreferenceManager ;
3232import android .support .v4 .app .Fragment ;
3333import android .support .v4 .app .FragmentActivity ;
3434import android .support .v4 .app .FragmentManager ;
3535import android .support .v4 .app .FragmentPagerAdapter ;
3636import android .support .v4 .view .ViewPager ;
37+ import android .text .Html ;
3738import android .util .SparseArray ;
39+ import android .view .LayoutInflater ;
3840import android .view .Menu ;
3941import android .view .MenuInflater ;
4042import android .view .MenuItem ;
43+ import android .view .View ;
4144import android .view .ViewGroup ;
4245import android .view .Window ;
4346import android .view .WindowManager ;
47+ import android .webkit .WebView ;
48+ import android .webkit .WebViewClient ;
4449import android .widget .HorizontalScrollView ;
4550import android .widget .ImageView ;
4651import android .widget .LinearLayout ;
5055import com .joanzapata .android .iconify .IconDrawable ;
5156import com .joanzapata .android .iconify .Iconify ;
5257
58+ import org .json .JSONArray ;
59+ import org .json .JSONException ;
60+ import org .json .JSONObject ;
61+
5362import java .lang .reflect .Method ;
54- import java .util . Date ;
63+ import java .text . NumberFormat ;
5564
5665import au .com .wallaceit .reddinator .R ;
5766import au .com .wallaceit .reddinator .Reddinator ;
67+ import au .com .wallaceit .reddinator .core .RedditData ;
5868import au .com .wallaceit .reddinator .core .ThemeManager ;
59- import au .com .wallaceit .reddinator .service .MailCheckService ;
6069import au .com .wallaceit .reddinator .ui .AccountFeedFragment ;
6170import au .com .wallaceit .reddinator .ui .SimpleTabsWidget ;
6271
6372public class AccountActivity extends FragmentActivity implements AccountFeedFragment .ActivityInterface {
6473
6574 private Reddinator global ;
66- private SharedPreferences prefs ;
6775 private MenuItem messageIcon ;
6876 private ActionBar actionBar ;
6977 private BroadcastReceiver inboxReceiver ;
@@ -74,6 +82,7 @@ public class AccountActivity extends FragmentActivity implements AccountFeedFrag
7482 public static final String ACTION_SAVED = "saved" ;
7583 public static final String ACTION_HIDDEN = "hidden" ;
7684 private String section = "overview" ;
85+ ThemeManager .Theme theme ;
7786
7887 /**
7988 * (non-Javadoc)
@@ -88,7 +97,6 @@ protected void onCreate(Bundle savedInstanceState) {
8897 getWindow ().requestFeature (WindowManager .LayoutParams .FLAG_LAYOUT_NO_LIMITS );
8998 getWindow ().requestFeature (WindowManager .LayoutParams .FLAG_LAYOUT_IN_SCREEN );
9099
91- prefs = PreferenceManager .getDefaultSharedPreferences (AccountActivity .this );
92100 resources = getResources ();
93101 // get actionbar and set home button, pad the icon
94102 actionBar = getActionBar ();
@@ -99,6 +107,8 @@ protected void onCreate(Bundle savedInstanceState) {
99107 if (view != null ) {
100108 view .setPadding (5 , 0 , 5 , 0 );
101109 }
110+ // set title with username and karma
111+ actionBar .setTitle (global .mRedditData .getUsername ());
102112 // set content view
103113 setContentView (R .layout .view_account );
104114 // Setup View Pager and widget
@@ -153,10 +163,19 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
153163 }
154164
155165 private void updateTheme (){
156- ThemeManager . Theme theme = getCurrentTheme ();
166+ theme = getCurrentTheme ();
157167 tabsIndicator .setBackgroundColor (Color .parseColor (theme .getValue ("header_color" )));
158168 tabsIndicator .setInidicatorColor (Color .parseColor (theme .getValue ("tab_indicator" )));
159169 tabsIndicator .setTextColor (Color .parseColor (theme .getValue ("header_text" )));
170+ updateSubtitle ();
171+ }
172+
173+ private void updateSubtitle (){
174+ String linkKarma = NumberFormat .getInstance ().format (global .mRedditData .getLinkKarma ());
175+ String commentKarma = NumberFormat .getInstance ().format (global .mRedditData .getCommentKarma ());
176+ actionBar .setSubtitle (
177+ Html .fromHtml ("<font color='" + theme .getValue ("votes_icon" ) + "'>" + linkKarma + "</font> - " +
178+ "<font color='" + theme .getValue ("comments_icon" ) + "'>" + commentKarma + "</font>" ));
160179 }
161180
162181 public ThemeManager .Theme getCurrentTheme (){
@@ -165,24 +184,8 @@ public ThemeManager.Theme getCurrentTheme(){
165184
166185 public void onResume (){
167186 super .onResume ();
168- // Register receiver & check for new messages if logged in, enabled and due
169- int checkPref = Integer .parseInt (prefs .getString ("mail_check_pref" , "300000" ));
170- if (global .mRedditData .isLoggedIn () || checkPref !=0 )
171- if ((global .mRedditData .getLastUserUpdateTime ()+checkPref )<(new Date ()).getTime ()) {
172- inboxReceiver = new BroadcastReceiver () {
173- @ Override
174- public void onReceive (Context context , Intent intent ) {
175- // update inbox indicator
176- setInboxIcon ();
177- }
178- };
179- IntentFilter filter = new IntentFilter ();
180- filter .addAction (MailCheckService .MAIL_CHECK_COMPLETE );
181- registerReceiver (inboxReceiver , filter );
182-
183- MailCheckService .checkMail (AccountActivity .this , MailCheckService .ACTIVITY_CHECK_ACTION );
184- }
185-
187+ // user info update refreshes both karma and message indicator
188+ triggerRefreshUserInfo ();
186189 setInboxIcon ();
187190 }
188191
@@ -207,6 +210,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
207210 inflater .inflate (R .menu .account_menu , menu );
208211 // set options menu view
209212 (menu .findItem (R .id .menu_submit )).setIcon (new IconDrawable (this , Iconify .IconValue .fa_pencil ).color (actionbarIconColor ).actionBarSize ());
213+ (menu .findItem (R .id .menu_karma )).setIcon (new IconDrawable (this , Iconify .IconValue .fa_star ).color (actionbarIconColor ).actionBarSize ());
210214 (menu .findItem (R .id .menu_viewonreddit )).setIcon (new IconDrawable (this , Iconify .IconValue .fa_globe ).color (actionbarIconColor ).actionBarSize ());
211215 (menu .findItem (R .id .menu_prefs )).setIcon (new IconDrawable (this , Iconify .IconValue .fa_wrench ).color (actionbarIconColor ).actionBarSize ());
212216 (menu .findItem (R .id .menu_about )).setIcon (new IconDrawable (this , Iconify .IconValue .fa_info_circle ).color (actionbarIconColor ).actionBarSize ());
@@ -262,6 +266,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
262266 startActivity (inboxIntent );
263267 break ;
264268
269+ case R .id .menu_karma :
270+ new LoadUserDetailsTask ().execute ();
271+ break ;
272+
265273 case R .id .menu_submit :
266274 Intent submitIntent = new Intent (AccountActivity .this , SubmitActivity .class );
267275 startActivity (submitIntent );
@@ -289,6 +297,136 @@ public boolean onOptionsItemSelected(MenuItem item) {
289297 return true ;
290298 }
291299
300+ private void showUserDetailsDialog (JSONObject [] data ){
301+ System .out .println (data [0 ].toString ());
302+ System .out .println (data [1 ].toString ());
303+ JSONArray trophies , karma ;
304+ try {
305+ karma = data [0 ].getJSONArray ("data" );
306+ } catch (JSONException e ) {
307+ e .printStackTrace ();
308+ karma = new JSONArray ();
309+ }
310+ try {
311+ trophies = data [1 ].getJSONObject ("data" ).getJSONArray ("trophies" );
312+ } catch (JSONException e ) {
313+ e .printStackTrace ();
314+ trophies = new JSONArray ();
315+ }
316+ String html = "" ;
317+ // build trophies
318+ html += "<h3>Trophies</h3><div style='text-align:center;'>" ;
319+ for (int i = 0 ; i <trophies .length (); i ++){
320+ try {
321+ JSONObject trophy = trophies .getJSONObject (i ).getJSONObject ("data" );
322+ String icon = trophy .getString ("icon_70" );
323+ String name = trophy .getString ("name" );
324+ html += "<div style='display:inline-block; min-width:100px; text-align: center; padding: 6px;'>" ;
325+ html += "<img src='" +icon +"' />" ;
326+ html += "<p style='margin-top:4px;'>" +name +"</p>" ;
327+ html += "</div>" ;
328+ } catch (JSONException e ) {
329+ e .printStackTrace ();
330+ }
331+ }
332+ html += "</div>" ;
333+ // build karma table
334+ html += "<h3 style='margin-top:4px;'>Karma by Subreddit</h3>" ;
335+ html += "<table style='margin:0;width:100%;'><thead><tr><th style='text-align:left;'>Subreddit</th><th>Links</th><th>Comments</th></tr></thead><tbody>" ;
336+ for (int i = 0 ; i <karma .length (); i ++){
337+ try {
338+ JSONObject subKarma = karma .getJSONObject (i );
339+ String subreddit = subKarma .getString ("sr" );
340+ String link = subKarma .getString ("link_karma" );
341+ String comment = subKarma .getString ("comment_karma" );
342+ html += "<tr><td>" +subreddit +"</td><td style='text-align:right;'>" +link +"</td><td style='text-align:right;'>" +comment +"</td></tr>" ;
343+ } catch (JSONException e ) {
344+ e .printStackTrace ();
345+ }
346+ }
347+ html += "</tbody></table>" ;
348+ // open dialog
349+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
350+ @ SuppressLint ("InflateParams" )
351+ View view = LayoutInflater .from (this ).inflate (R .layout .dialog_account_details , null );
352+ WebView wv = (WebView ) view .findViewById (R .id .webView );
353+ builder .setTitle (global .mRedditData .getUsername ());
354+ builder .setView (view );
355+ builder .setCancelable (true );
356+ wv .setWebViewClient (new WebViewClient ());
357+ wv .loadData (html , "text/html" , "UTF-8" );
358+ builder .show ();
359+ }
360+
361+ private class LoadUserDetailsTask extends AsyncTask <Void , Void , JSONObject []>{
362+ private RedditData .RedditApiException exception = null ;
363+ ProgressDialog progressDialog ;
364+
365+ protected void onPreExecute () {
366+ progressDialog = ProgressDialog .show (AccountActivity .this , resources .getString (R .string .loading ), resources .getString (R .string .one_moment ), true );
367+ }
368+
369+ @ Override
370+ protected JSONObject [] doInBackground (Void ... strings ) {
371+ try {
372+ JSONObject karma = global .mRedditData .getKarmaBreakdown ();
373+ JSONObject trophies = global .mRedditData .getTrophies ();
374+ return new JSONObject []{karma , trophies };
375+ } catch (RedditData .RedditApiException e ) {
376+ e .printStackTrace ();
377+ exception = e ;
378+ return null ;
379+ }
380+ }
381+
382+ @ Override
383+ protected void onPostExecute (JSONObject [] result ) {
384+ progressDialog .dismiss ();
385+ if (result !=null ) {
386+ showUserDetailsDialog (result );
387+ } else {
388+ // show error
389+ Toast .makeText (global .getApplicationContext (), "Failed to load karma breakdown: " + exception .getMessage (), Toast .LENGTH_LONG ).show ();
390+ }
391+ }
392+ }
393+
394+ // don't update this more than once a minute
395+ public void triggerRefreshUserInfo (){
396+ long now = System .currentTimeMillis ();
397+ long last = global .mRedditData .getLastUserUpdateTime ();
398+ if ((now -last )>60000 ){
399+ new RefreshUserInfoTask ().execute ();
400+ }
401+ }
402+
403+ private class RefreshUserInfoTask extends AsyncTask <Void , Void , Boolean >{
404+ private RedditData .RedditApiException exception = null ;
405+
406+ @ Override
407+ protected Boolean doInBackground (Void ... strings ) {
408+ try {
409+ global .mRedditData .updateUserInfo ();
410+ return true ;
411+ } catch (RedditData .RedditApiException e ) {
412+ e .printStackTrace ();
413+ exception = e ;
414+ return false ;
415+ }
416+ }
417+
418+ @ Override
419+ protected void onPostExecute (Boolean result ) {
420+ if (result ) {
421+ updateSubtitle ();
422+ setInboxIcon ();
423+ } else {
424+ // show error
425+ Toast .makeText (global .getApplicationContext (), "Failed to refresh user info: " + exception .getMessage (), Toast .LENGTH_LONG ).show ();
426+ }
427+ }
428+ }
429+
292430 public void setTitleText (final String title ){
293431 runOnUiThread (new Runnable () {
294432 @ Override
0 commit comments