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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
package de.blinkt.openvpn.fragments;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.*;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import com.android.vending.billing.IInAppBillingService;
import de.blinkt.openvpn.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
public class AboutFragment extends Fragment implements View.OnClickListener {
public static final String INAPPITEM_TYPE_INAPP = "inapp";
public static final String RESPONSE_CODE = "RESPONSE_CODE";
private static final int DONATION_CODE = 12;
private static final int BILLING_RESPONSE_RESULT_OK = 0;
private static final String RESPONSE_BUY_INTENT = "BUY_INTENT";
private static final String[] donationSkus = { "donation1eur", "donation2eur", "donation5eur", "donation10eur",
"donation1337eur","donation23eur","donation25eur",};
IInAppBillingService mService;
Hashtable<View, String> viewToProduct = new Hashtable<View, String>();
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
initGooglePlayDonation();
}
};
private void initGooglePlayDonation() {
new Thread("queryGMSInApp") {
@Override
public void run() {
initGMSDonateOptions();
}
}.start();
}
private TextView gmsTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().bindService(new
Intent("com.android.vending.billing.InAppBillingService.BIND"),
mServiceConn, Context.BIND_AUTO_CREATE);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mServiceConn != null) {
getActivity().unbindService(mServiceConn);
}
}
private void initGMSDonateOptions() {
try {
int billingSupported = mService.isBillingSupported(3, getActivity().getPackageName(), INAPPITEM_TYPE_INAPP);
if (billingSupported != BILLING_RESPONSE_RESULT_OK) {
Log.i("OpenVPN", "Play store billing not supported");
return;
}
ArrayList skuList = new ArrayList();
Collections.addAll(skuList, donationSkus);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle ownedItems = mService.getPurchases(3, getActivity().getPackageName(), INAPPITEM_TYPE_INAPP, null);
if (ownedItems.getInt(RESPONSE_CODE) != BILLING_RESPONSE_RESULT_OK)
return;
final ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
Bundle skuDetails = mService.getSkuDetails(3, getActivity().getPackageName(), INAPPITEM_TYPE_INAPP, querySkus);
if (skuDetails.getInt(RESPONSE_CODE) != BILLING_RESPONSE_RESULT_OK)
return;
final ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
createPlayBuyOptions(ownedSkus, responseList);
}
});
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
private static class SkuResponse {
String title;
String price;
SkuResponse(String p, String t)
{
title=t;
price=p;
}
}
private void createPlayBuyOptions(ArrayList<String> ownedSkus, ArrayList<String> responseList) {
try {
Vector<Pair<String,String>> gdonation = new Vector<Pair<String, String>>();
gdonation.add(new Pair<String, String>(getString(R.string.donatePlayStore),null));
HashMap<String, SkuResponse> responseMap = new HashMap<String, SkuResponse>();
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
responseMap.put(
object.getString("productId"),
new SkuResponse(
object.getString("price"),
object.getString("title")));
}
for (String sku: donationSkus)
if (responseMap.containsKey(sku))
gdonation.add(getSkuTitle(sku,
responseMap.get(sku).title, responseMap.get(sku).price, ownedSkus));
String gmsTextString="";
for(int i=0;i<gdonation.size();i++) {
if(i==1)
gmsTextString+= " ";
else if(i>1)
gmsTextString+= ", ";
gmsTextString+=gdonation.elementAt(i).first;
}
SpannableString gmsText = new SpannableString(gmsTextString);
int lStart = 0;
int lEnd=0;
for(Pair<String, String> item:gdonation){
lEnd = lStart + item.first.length();
if (item.second!=null) {
final String mSku = item.second;
ClickableSpan cspan = new ClickableSpan()
{
@Override
public void onClick(View widget) {
triggerBuy(mSku);
}
};
gmsText.setSpan(cspan,lStart,lEnd,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
lStart = lEnd+2; // Account for ", " between items
}
if(gmsTextView !=null) {
gmsTextView.setText(gmsText);
gmsTextView.setMovementMethod(LinkMovementMethod.getInstance());
gmsTextView.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private Pair<String,String> getSkuTitle(final String sku, String title, String price, ArrayList<String> ownedSkus) {
String text;
if (ownedSkus.contains(sku))
return new Pair<String,String>(getString(R.string.thanks_for_donation, price),null);
if (price.contains("€")|| price.contains("\u20ac")) {
text= title;
} else {
text = String.format(Locale.getDefault(), "%s (%s)", title, price);
}
//return text;
return new Pair<String,String>(price, sku);
}
private void triggerBuy(String sku) {
try {
Bundle buyBundle
= mService.getBuyIntent(3, getActivity().getPackageName(),
sku, INAPPITEM_TYPE_INAPP, "Thanks for the donation! :)");
if (buyBundle.getInt(RESPONSE_CODE) == BILLING_RESPONSE_RESULT_OK) {
PendingIntent buyIntent = (PendingIntent) buyBundle.getParcelable(RESPONSE_BUY_INTENT);
getActivity().startIntentSenderForResult(buyIntent.getIntentSender(), DONATION_CODE, new Intent(),
0, 0, 0);
}
} catch (RemoteException e) {
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.about, container, false);
TextView ver = (TextView) v.findViewById(R.id.version);
String version;
String name = "Openvpn";
try {
PackageInfo packageinfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
version = packageinfo.versionName;
name = getString(R.string.app);
} catch (NameNotFoundException e) {
version = "error fetching version";
}
ver.setText(getString(R.string.version_info, name, version));
TextView paypal = (TextView) v.findViewById(R.id.donatestring);
String donatetext = getActivity().getString(R.string.donatewithpaypal);
Spanned htmltext = Html.fromHtml(donatetext);
paypal.setText(htmltext);
paypal.setMovementMethod(LinkMovementMethod.getInstance());
gmsTextView = (TextView) v.findViewById(R.id.donategms);
/* recreating view without onCreate/onDestroy cycle */
if (mService!=null)
initGooglePlayDonation();
TextView translation = (TextView) v.findViewById(R.id.translation);
// Don't print a text for myself
if (getString(R.string.translationby).contains("Arne Schwabe"))
translation.setText("");
else
translation.setText(R.string.translationby);
WebView wv = (WebView)v.findViewById(R.id.webView);
wv.loadUrl("file:///android_asset/full_licenses.html");
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (mService!=null)
initGooglePlayDonation();
}
@Override
public void onClick(View v) {
}
}
|