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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
| public class Application extends android.app.Application {
VKAccessTokenTracker vkAccessTokenTracker = new VKAccessTokenTracker() {
@Override
public void onVKAccessTokenChanged(@Nullable VKAccessToken oldToken, @Nullable VKAccessToken newToken) {
if (newToken == null) {
Toast.makeText(Application.this, "AccessToken invalidated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Application.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
};
@Override
public void onCreate() {
super.onCreate();
vkAccessTokenTracker.startTracking();
VKSdk.initialize(this);
}
}
public class LoginActivity extends FragmentActivity {
private boolean isResumed = false;
/**
* Scope is set of required permissions for your application
*
* @see <a href="https://vk.com/dev/permissions">vk.com api permissions documentation</a>
*/
private static final String[] sMyScope = new String[]{
VKScope.WALL,
VKScope.NOHTTPS,
VKScope.VIDEO,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
VKSdk.wakeUpSession(this, new VKCallback<VKSdk.LoginState>() {
@Override
public void onResult(VKSdk.LoginState res) {
if (isResumed) {
switch (res) {
case LoggedOut:
showLogin();
break;
case LoggedIn:
showLogout();
break;
case Pending:
break;
case Unknown:
break;
}
}
}
@Override
public void onError(VKError error) {
}
});
// String[] fingerprint = VKUtil.getCertificateFingerprint(this, this.getPackageName());
// Log.d("Fingerprint", fingerprint[0]);
}
private void showLogout() {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, new LogoutFragment())
.commitAllowingStateLoss();
}
private void showLogin() {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, new LoginFragment())
.commitAllowingStateLoss();
}
@Override
protected void onResume() {
super.onResume();
isResumed = true;
if (VKSdk.isLoggedIn()) {
showLogout();
} else {
showLogin();
}
}
@Override
protected void onPause() {
isResumed = false;
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
VKCallback<VKAccessToken> callback = new VKCallback<VKAccessToken>() {
@Override
public void onResult(VKAccessToken res) {
// User passed Authorization
startMainActivity();
}
@Override
public void onError(VKError error) {
// User didn't pass Authorization
}
};
if (!VKSdk.onActivityResult(requestCode, resultCode, data, callback)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void startMainActivity() {
startActivity(new Intent(this, MainActivity.class));
}
public static class LoginFragment extends android.support.v4.app.Fragment {
public LoginFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, container, false);
v.findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
VKSdk.login(getActivity(), sMyScope);
}
});
return v;
}
}
public static class LogoutFragment extends android.support.v4.app.Fragment {
public LogoutFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_logout, container, false);
v.findViewById(R.id.continue_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((LoginActivity) getActivity()).startMainActivity();
}
});
v.findViewById(R.id.logout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
VKSdk.logout();
if (!VKSdk.isLoggedIn()) {
((LoginActivity) getActivity()).showLogin();
}
}
});
return v;
}
}
} |