Skip to content

Commit

Permalink
iap for additional styles
Browse files Browse the repository at this point in the history
  • Loading branch information
naman14 committed Feb 22, 2017
1 parent bc56d0a commit 273eda0
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.anjlab.android.iab.v3.SkuDetails;
import com.anjlab.android.iab.v3.TransactionDetails;
import com.naman14.timber.R;
import com.naman14.timber.utils.PreferencesUtility;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -42,6 +43,8 @@ public class DonateActivity extends BaseThemedActivity implements BillingProcess
private ProgressBar progressBar;
private TextView status;

private String action = "support";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -51,11 +54,17 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Donate");
getSupportActionBar().setTitle("Support development");
action = getIntent().getAction();

productListView = (LinearLayout) findViewById(R.id.product_list);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
status = (TextView) findViewById(R.id.donation_status);

if (action != null && action.equals("restore")) {
status.setText("Restoring purchases..");
}

bp = new BillingProcessor(this, getString(R.string.play_billing_license_key), this);

}
Expand All @@ -64,7 +73,8 @@ protected void onCreate(Bundle savedInstanceState) {
public void onBillingInitialized() {
readyToPurchase = true;
checkStatus();
getProducts();
if (!(action != null && action.equals("restore")))
getProducts();
}

@Override
Expand All @@ -73,7 +83,7 @@ public void onProductPurchased(String productId, TransactionDetails details) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(DonateActivity.this, "Thanks for your donation!", Toast.LENGTH_SHORT).show();
Toast.makeText(DonateActivity.this, "Thanks for your support!", Toast.LENGTH_SHORT).show();
}
});
}
Expand Down Expand Up @@ -119,7 +129,18 @@ protected Boolean doInBackground(Void... voids) {
protected void onPostExecute(Boolean b) {
super.onPostExecute(b);
if (b) {
status.setText("Thanks for your donation!");
PreferencesUtility.getInstance(DonateActivity.this).setFullUnlocked(true);
status.setText("Thanks for your support!");
if (action!=null && action.equals("restore")) {
status.setText("Your purchases has been restored. Thanks for your support");
}
if (getSupportActionBar() != null)
getSupportActionBar().setTitle("Support development");
} else {
if (action!=null && action.equals("restore")) {
status.setText("No previous purchase found");
getProducts();
}
}
}
}.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package com.naman14.timber.subfragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
Expand All @@ -26,7 +28,10 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.naman14.timber.R;
import com.naman14.timber.activities.DonateActivity;
import com.naman14.timber.utils.Constants;
import com.naman14.timber.utils.NavigationUtils;
import com.naman14.timber.utils.PreferencesUtility;
Expand All @@ -39,7 +44,7 @@ public class SubStyleSelectorFragment extends Fragment {
SharedPreferences preferences;
LinearLayout currentStyle;
View foreground;
ImageView styleImage;
ImageView styleImage, imgLock;

public static SubStyleSelectorFragment newInstance(int pageNumber, String what) {
SubStyleSelectorFragment fragment = new SubStyleSelectorFragment();
Expand All @@ -57,12 +62,22 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

TextView styleName = (TextView) rootView.findViewById(R.id.style_name);
styleName.setText(String.valueOf(getArguments().getInt(ARG_PAGE_NUMBER) + 1));
preferences = getActivity().getSharedPreferences(Constants.FRAGMENT_ID, Context.MODE_PRIVATE);

styleImage = (ImageView) rootView.findViewById(R.id.style_image);
imgLock = (ImageView) rootView.findViewById(R.id.img_lock);

styleImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPreferences();
if (getArguments().getInt(ARG_PAGE_NUMBER) == 4) {
if (isUnlocked()) {
setPreferences();
} else {
showPurchaseDialog();
}
} else
setPreferences();
}
});

Expand All @@ -80,9 +95,6 @@ public void onClick(View view) {
styleImage.setImageResource(R.drawable.timber_4_nowplaying_x);
break;
case 4:
styleImage.setImageResource(R.drawable.timber_4_nowplaying_x);
break;
case 5:
styleImage.setImageResource(R.drawable.timber_5_nowplaying_x);
break;
}
Expand All @@ -92,11 +104,51 @@ public void onClick(View view) {

setCurrentStyle();

if (getArguments().getInt(ARG_PAGE_NUMBER) == 4 && !isUnlocked())
imgLock.setVisibility(View.VISIBLE);
else imgLock.setVisibility(View.GONE);

return rootView;
}

private boolean isUnlocked() {
return getActivity() != null && PreferencesUtility.getInstance(getActivity()).fullUnlocked();
}

@Override
public void onResume() {
super.onResume();
if (!isUnlocked())
imgLock.setVisibility(View.VISIBLE);
else imgLock.setVisibility(View.GONE);
}

private void showPurchaseDialog() {
MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
.title("Purchase")
.content("This now playing style is available after a one time purchase of any amount. Support development and unlock this style?")
.positiveText("Support development")
.neutralText("Restore purchases")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
startActivity(new Intent(getActivity(), DonateActivity.class));
dialog.dismiss();
}
}).onNeutral(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent(getActivity(), DonateActivity.class);
intent.putExtra("title", "Restoring purchases..");
intent.setAction("restore");
startActivity(intent);
dialog.dismiss();
}
})
.show();
}

public void setCurrentStyle() {
preferences = getActivity().getSharedPreferences(Constants.FRAGMENT_ID, Context.MODE_PRIVATE);
String fragmentID = preferences.getString(Constants.NOWPLAYING_FRAGMENT_ID, Constants.TIMBER3);

if (getArguments().getInt(ARG_PAGE_NUMBER) == NavigationUtils.getIntForCurrentNowplaying(fragmentID)) {
Expand Down Expand Up @@ -140,5 +192,4 @@ private String getStyleForPageNumber() {
}



}
11 changes: 11 additions & 0 deletions app/src/main/java/com/naman14/timber/utils/PreferencesUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class PreferencesUtility {
private static final String TOGGLE_XPOSED_TRACKSELECTOR = "toggle_xposed_trackselector";
public static final String LAST_ADDED_CUTOFF = "last_added_cutoff";
public static final String GESTURES = "gestures";
public static final String FULL_UNLOCKED = "full_version_unlocked";

private static PreferencesUtility sInstance;

Expand Down Expand Up @@ -235,5 +236,15 @@ public void storeLastFolder(String path) {
public String getLastFolder() {
return mPreferences.getString(LAST_FOLDER, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath());
}

public boolean fullUnlocked() {
return mPreferences.getBoolean(FULL_UNLOCKED, false);
}

public void setFullUnlocked(final boolean b) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(FULL_UNLOCKED, b);
editor.apply();
}
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_donate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:textSize="16sp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Show your appreciation and support the development of Timber by donating some dollars to me "/>
android:text="Show your appreciation and support the development of Timber. Purchasing any of the below product also unlocks additional now playing styles."/>


<ScrollView
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/fragment_style_selector_pager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@

</LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_lock_outline_white_24dp"
android:id="@+id/img_lock"
android:layout_gravity="center"
android:visibility="gone"/>

</FrameLayout>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_donate_product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_donate"
android:text="Donate"
android:text="Purchase"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
/>
Expand Down

0 comments on commit 273eda0

Please sign in to comment.