-
Notifications
You must be signed in to change notification settings - Fork 12
/
UserOverviewActivity.java
125 lines (103 loc) · 4.57 KB
/
UserOverviewActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package net.dean.jraw.android.example;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.dean.jraw.RedditClient;
import net.dean.jraw.models.Account;
import net.dean.jraw.models.PersistedAuthData;
import net.dean.jraw.oauth.DeferredPersistentTokenStore;
import java.lang.ref.WeakReference;
import java.text.NumberFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* Shows some very simple information about the currently authenticated user. Currently, it shows
* their username, link karma, comment karma, access token expiration, and if we have a refresh
* token for them.
*/
public class UserOverviewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_overview);
// Fetch the user's account information
new GetUserInfoTask(this).execute(App.getAccountHelper().getReddit());
}
@Override
public void onBackPressed() {
super.onBackPressed();
// Provide the user two ways to log out: the "log out" button, and exiting this activity
onLogout(null);
}
private void show(Account account) {
// Basics
textView(R.id.username).setText(account.getName());
textView(R.id.linkKarma).setText(getString(R.string.link_karma, formatInt(account.getLinkKarma())));
textView(R.id.commentKarma).setText(getString(R.string.comment_karma, formatInt(account.getCommentKarma())));
// OAuth2 stuff
DeferredPersistentTokenStore tokenStore = App.getTokenStore();
PersistedAuthData data = tokenStore.inspect(account.getName());
if (data != null) {
if (data.getLatest() != null) {
// Calculate the amount of minutes in which the OAuthData will expire
long diffMillis = data.getLatest().getExpiration().getTime() - new Date().getTime();
long diffMinutes = TimeUnit.MINUTES.convert(diffMillis, TimeUnit.MILLISECONDS);
// Update the TextView with this information
textView(R.id.accessTokenStatus).setText(getString(R.string.access_token_status, diffMinutes));
}
// Show whether or not the data includes a refresh token
textView(R.id.refreshToken).setText(data.getRefreshToken() == null ?
R.string.no_refresh_token : R.string.refresh_token);
}
}
private TextView textView(@IdRes int id) {
return (TextView) findViewById(id);
}
private static String formatInt(int n) {
return NumberFormat.getInstance().format(n);
}
public void onLogout(View view) {
// All this does is remove the current RedditClient reference. If we tried to do
// App.getAccountHelper().getReddit(), it would throw an IllegalStateException.
App.getAccountHelper().logout();
finish();
}
private static final class GetUserInfoTask extends AsyncTask<RedditClient, Void, Account> {
// Use a WeakReference to avoid leaking a Context
private final WeakReference<UserOverviewActivity> activity;
GetUserInfoTask(UserOverviewActivity activity) {
this.activity = new WeakReference<>(activity);
}
@Override
protected void onPreExecute() {
ProgressBar progressBar = getProgressBar();
if (progressBar != null)
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Account doInBackground(RedditClient... redditClients) {
return redditClients[0].me().about();
}
@Override
protected void onPostExecute(Account account) {
// Display the fetched account if the Activity still exists
UserOverviewActivity activity = this.activity.get();
if (activity != null)
activity.show(account);
// Prefer INVISIBLE instead of GONE so everything doesn't get shifted up a few pixels
// once loading is done
ProgressBar progressBar = getProgressBar();
if (progressBar != null)
progressBar.setVisibility(View.INVISIBLE);
}
private ProgressBar getProgressBar() {
if (this.activity.get() != null)
return this.activity.get().findViewById(R.id.progress);
return null;
}
}
}