Skip to content

Commit bb989bf

Browse files
committed
[app] Introduce CheckoutApplication and prepare Billing
As there should be only one instance of Billing in the app it is instantiated in the Application class. It can later be used via `Activity#getApplication().getBilling()`. Encryption class is added in this commit in order to encrypt/decrypt the key.
1 parent 0d95bf5 commit bb989bf

File tree

9 files changed

+91
-0
lines changed

9 files changed

+91
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
<manifest
2424
xmlns:android="http://schemas.android.com/apk/res/android"
25+
xmlns:tools="http://schemas.android.com/tools"
2526
package="org.solovyev.android.checkout.app">
2627

2728
<supports-screens android:smallScreens="true" />
@@ -32,4 +33,12 @@
3233

3334
<uses-permission android:name="com.android.vending.BILLING" />
3435

36+
<application
37+
android:name=".CheckoutApplication"
38+
android:allowBackup="true"
39+
android:icon="@drawable/ic_launcher"
40+
android:label="@string/app_name"
41+
android:supportsRtl="true"
42+
tools:ignore="GoogleAppIndexingWarning" />
43+
3544
</manifest>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.solovyev.android.checkout.app;
2+
3+
import org.solovyev.android.checkout.Billing;
4+
5+
import android.app.Application;
6+
7+
import javax.annotation.Nonnull;
8+
9+
public class CheckoutApplication extends Application {
10+
11+
@Nonnull
12+
private final Billing mBilling = new Billing(this, new Billing.DefaultConfiguration() {
13+
@Nonnull
14+
@Override
15+
public String getPublicKey() {
16+
// encrypted public key of the app. Plain version can be found in Google Play's Developer
17+
// Console in Service & APIs section under "YOUR LICENSE KEY FOR THIS APPLICATION" title.
18+
// A naive encryption algorithm is used to "protect" the key. See more about key protection
19+
// here: https://developer.android.com/google/play/billing/billing_best_practices.html#key
20+
final String s = "PixnMSYGLjg7Ah0xDwYILlVZUy0sIiBoMi4jLDcoXTcNLiQjKgtlIC48NiRcHxwKHEcYEyZrPyMWXFRpV10VES9ENz" +
21+
"g1Hj06HTV1MCAHJlpgEDcmOxFDEkA8OiQRKjEQDxhRWVVEMBYmNl1AJghcKUAYVT15KSQgBQABMgwqKSlqF1gZBA4fAw5rMyxKI" +
22+
"w9LJFc7AhxZGjoPATgRUiUjKSsOWyRKDi4nIA9lKgAGOhMLDF06CwoKGFR6Wj0hGwReS10NXzQTIREhKlkuMz4XDTwUQjRCJUA+" +
23+
"VjQVPUIoPicOLQJCLxs8RjZnJxY1OQNLKgQCPj83AyBEFSAJEk5UClYjGxVLNBU3FS4DCztENQMuOk5rFVclKz88AAApPgADGFx" +
24+
"EEV5eQAF7QBhdQEE+Bzc5MygCAwlEFzclKRB7FB0uFgwPKgAvLCk2OyFiKxkgIy8BBQYjFy4/E1ktJikrEVlKJVYIHh16NDwtDC" +
25+
"U0Vg8JNzoQBwQWOwk1GzZ4FT8fWicwITcRJi8=";
26+
return Encryption.decrypt(s, "se.solovyev@gmail.com");
27+
}
28+
});
29+
30+
@Nonnull
31+
public Billing getBilling() {
32+
return mBilling;
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.solovyev.android.checkout.app;
2+
3+
import android.util.Base64;
4+
5+
import javax.annotation.Nonnull;
6+
7+
/**
8+
* Simple (and stupid) encryption algorithm. Should not be used in real apps.
9+
*/
10+
final class Encryption {
11+
12+
@Nonnull
13+
static String decrypt(@Nonnull String message, @Nonnull String salt) {
14+
return xor(new String(Base64.decode(message, 0)), salt);
15+
}
16+
17+
@Nonnull
18+
static String encrypt(@Nonnull String message, @Nonnull String salt) {
19+
return new String(Base64.encode(xor(message, salt).getBytes(), 0));
20+
}
21+
22+
/**
23+
* Encrypts or decrypts a base-64 string using a XOR cipher.
24+
*/
25+
@Nonnull
26+
private static String xor(@Nonnull String message, @Nonnull String salt) {
27+
final char[] m = message.toCharArray();
28+
final int ml = m.length;
29+
30+
final char[] s = salt.toCharArray();
31+
final int sl = s.length;
32+
33+
final char[] res = new char[ml];
34+
for (int i = 0; i < ml; i++) {
35+
res[i] = (char) (m[i] ^ s[i % sl]);
36+
}
37+
return new String(res);
38+
}
39+
40+
}
5.67 KB
Loading
2.97 KB
Loading
7.33 KB
Loading
13.8 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Checkout</string>
4+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Checkout</string>
4+
</resources>

0 commit comments

Comments
 (0)