Skip to content

Commit

Permalink
implementing in-app billing
Browse files Browse the repository at this point in the history
  • Loading branch information
m7mdyahia committed Dec 26, 2017
1 parent 8ce45cb commit 8fa8d5e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ public void onOkPressed() {
}

private void showBookInformationFailurMessage() {
DialogFragment dialogFragment = new CloseDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "CloseDialogFragment");
if (getWindow() != null && getWindow().getDecorView().isShown()) {
DialogFragment dialogFragment = new CloseDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "CloseDialogFragment");
} else
Toast.makeText(this, R.string.book_information_error_close, Toast.LENGTH_LONG).show();
}

private class InitialSetupThread extends Thread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ public class DownloadFileConstants {
public static final String ONLINE_DATABASE_NAME = "main" + "." + DATABASE_FILE_EXTENSTION;
public static final String COMPRESSION_EXTENTION = "zip";
public static final String ISLAMIC_LIBRARY_BASE_DIRECTORY = "IslamicLibrary";
private static final String DOMAIN = "http://booksapi.islam-db.com";
private static final String BASE_URL = DOMAIN + "/data";
public static final String UNCOMPRESSED_BASE_BOOK_URL = BASE_URL + URL_SEPARATOR + "books";
public static final String BOOK_INFORMATION_URL = BASE_URL + URL_SEPARATOR + ONLINE_DATABASE_NAME;
public static final String COMPRESSED_BASE_BOOK_URL = BASE_URL + URL_SEPARATOR + "cbooks";
// http://booksapi.islam-db.com/data/cbooks/main.zip
public static final String COMPRESSED_BOOK_INFORMATION_URL = COMPRESSED_BASE_BOOK_URL + URL_SEPARATOR + "main" + "." + COMPRESSION_EXTENTION;
// public static final String COMPRESSED_BOOK_INFORMATION_URL = COMPRESSED_BASE_BOOK_URL + URL_SEPARATOR + "main111" + "." + COMPRESSION_EXTENTION;
public static final String PREF_APP_LOCATION = "custom_app_location_pref";
public static final String PREF_SDCARDPERMESSION_DIALOG_DISPLAYED = "PREF_SDCARD_PERMISSION_DIALOG_DISPLAYED";
public static final String SHAMELA_BOOKS_DIR = "shamela_books";
private static final String DOMAIN = "http://booksapi.islam-db.com";
private static final String BASE_URL = DOMAIN + "/data";
public static final String UNCOMPRESSED_BASE_BOOK_URL = BASE_URL + URL_SEPARATOR + "books";
public static final String BOOK_INFORMATION_URL = BASE_URL + URL_SEPARATOR + ONLINE_DATABASE_NAME;
public static final String COMPRESSED_BASE_BOOK_URL = BASE_URL + URL_SEPARATOR + "cbooks";

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -30,6 +31,8 @@
import java.util.Collections;
import java.util.List;

import timber.log.Timber;

import static com.fekracomputers.islamiclibrary.browsing.activity.BrowsingActivity.KEY_NUMBER_OF_BOOKS_TO_DONLOAD;
import static com.fekracomputers.islamiclibrary.download.model.DownloadsConstants.BROADCAST_ACTION;
import static com.fekracomputers.islamiclibrary.download.model.DownloadsConstants.EXTRA_NOTIFY_WITHOUT_BOOK_ID;
Expand All @@ -40,11 +43,10 @@ public class DownloadProgressActivity extends AppCompatActivity implements Cance
private static final int CANCELLED_DOWNLOAD_TYPE = 0;
private static final int FINISHED_DOWNLOAD_TYPE = 1;
private static final int ZERO_DOWNLOAD_TYPE = 2;
DownlandProgressRecyclerViewAdapter bookmarkRecyclerViewAdapter;
@Nullable
DownlandProgressRecyclerViewAdapter downlandProgressRecyclerViewAdapter;
private RecyclerView recyclerView;
private DownloadProgressAsncTask downloadProgressAsncTask;
private Menu mMenu;
private BooksInformationDbHelper booksInformationDbHelper;
private boolean mShowCancelAll = false;

@Override
Expand All @@ -64,18 +66,26 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView = findViewById(R.id.recyclerView);


booksInformationDbHelper = BooksInformationDbHelper.getInstance(this);
ArrayList<Long> downloads = booksInformationDbHelper.getPendingDownloads();

if (downloads.size() != 0) {
mShowCancelAll=true;
ArrayList<Long> downloads;
if (BooksInformationDbHelper.databaseFileExists()) {
BooksInformationDbHelper booksInformationDbHelper = BooksInformationDbHelper.getInstance(this);
if (booksInformationDbHelper != null) {
downloads = booksInformationDbHelper.getPendingDownloads();
if (downloads.size() != 0) {
mShowCancelAll = true;
downloadProgressAsncTask = new DownloadProgressAsncTask();
downloadProgressAsncTask.execute();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
} else {
showAlternativeView(ZERO_DOWNLOAD_TYPE);
}
}
} else {//downloading book information
mShowCancelAll = false;
downloadProgressAsncTask = new DownloadProgressAsncTask();
downloadProgressAsncTask.execute(downloads.toArray(new Long[0]));
downloadProgressAsncTask.execute();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

} else {
showAlternativeView(ZERO_DOWNLOAD_TYPE);
}
}

Expand Down Expand Up @@ -122,10 +132,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
return (true);
case R.id.cancel_all_downloads:
Bundle CancelDownloadDialogFragmentBundle = new Bundle();
CancelDownloadDialogFragmentBundle.putInt(KEY_NUMBER_OF_BOOKS_TO_DONLOAD, bookmarkRecyclerViewAdapter.getItemCount());
DialogFragment CancelDownloadDialogFragment = new CancelDownloadDialogFragment();
CancelDownloadDialogFragment.setArguments(CancelDownloadDialogFragmentBundle);
CancelDownloadDialogFragment.show(getSupportFragmentManager(), "CancelDownloadDialogFragment");
if (downlandProgressRecyclerViewAdapter != null) {
CancelDownloadDialogFragmentBundle.putInt(KEY_NUMBER_OF_BOOKS_TO_DONLOAD,
downlandProgressRecyclerViewAdapter.getItemCount());

DialogFragment CancelDownloadDialogFragment = new CancelDownloadDialogFragment();
CancelDownloadDialogFragment.setArguments(CancelDownloadDialogFragmentBundle);
CancelDownloadDialogFragment.show(getSupportFragmentManager(), "CancelDownloadDialogFragment");
} else {
Timber.d("downlandProgressRecyclerViewAdapter null");
}
return true;
}

Expand All @@ -142,7 +158,16 @@ private void showProgress() {
relativeLayout.setVisibility(View.VISIBLE);
}

private class DownloadProgressAsncTask extends AsyncTask<Long, DownloadInfoUpdate, DownloadInfoUpdate> {
private void cancelMultipleDownloads(Cursor c, int columnIndex) {
if (BooksInformationDbHelper.databaseFileExists()) {
BooksInformationDbHelper booksInformationDbHelper = BooksInformationDbHelper.getInstance(this);
if (booksInformationDbHelper != null) {
booksInformationDbHelper.cancelMultipleDownloads(c, columnIndex);
}
}
}

private class DownloadProgressAsncTask extends AsyncTask<Void, DownloadInfoUpdate, DownloadInfoUpdate> {

private List<DownloadInfo> mOldDownloadList;
private boolean mFirstTime;
Expand All @@ -151,19 +176,16 @@ public DownloadProgressAsncTask() {
this.mFirstTime = true;
}


@Override
protected DownloadInfoUpdate doInBackground(Long... ids) {
protected DownloadInfoUpdate doInBackground(Void... voids) {
//Convert Long[] to long[]
long candidateWatchedIds[] = new long[ids.length];
for (int i = 0; i < ids.length; i++) {
candidateWatchedIds[i] = ids[i];
}

while (true) {

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query BooksDownloadQuery = new DownloadManager.Query();
BooksDownloadQuery.setFilterById(candidateWatchedIds);

BooksDownloadQuery.setFilterByStatus(DownloadManager.STATUS_RUNNING |
DownloadManager.STATUS_PAUSED |
DownloadManager.STATUS_PENDING);
Expand Down Expand Up @@ -214,15 +236,14 @@ protected DownloadInfoUpdate doInBackground(Long... ids) {
non_complete_query.setFilterByStatus(DownloadManager.STATUS_FAILED |
DownloadManager.STATUS_PENDING |
DownloadManager.STATUS_RUNNING);
non_complete_query.setFilterById(candidateWatchedIds);
Cursor c = downloadManager.query(non_complete_query);
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_ID);
//TODO this loop may cause problems if a download completed and the broadcast is triggered before we cancel it
while (c.moveToNext()) {
long enquId = c.getLong(columnIndex);
downloadManager.remove(enquId);
}
booksInformationDbHelper.cancelMultipleDownloads(c, columnIndex);
DownloadProgressActivity.this.cancelMultipleDownloads(c, columnIndex);
Intent localIntent =
new Intent(BROADCAST_ACTION)
.putExtra(EXTRA_NOTIFY_WITHOUT_BOOK_ID, true);
Expand All @@ -243,22 +264,26 @@ protected DownloadInfoUpdate doInBackground(Long... ids) {
protected void onProgressUpdate(DownloadInfoUpdate... diffResult) {
if (diffResult[0].type == CANCELLED_DOWNLOAD_TYPE) {
showProgress();
} else if (!mFirstTime) {
} else if (!(mFirstTime || downlandProgressRecyclerViewAdapter == null)) {
updateAdaperAndDipatchChanges(diffResult[0]);
} else {
bookmarkRecyclerViewAdapter = new DownlandProgressRecyclerViewAdapter(DownloadProgressActivity.this, mOldDownloadList);
bookmarkRecyclerViewAdapter.setHasStableIds(true);
recyclerView.setAdapter(bookmarkRecyclerViewAdapter);
downlandProgressRecyclerViewAdapter = new DownlandProgressRecyclerViewAdapter(
DownloadProgressActivity.this,
mOldDownloadList);
downlandProgressRecyclerViewAdapter.setHasStableIds(true);
recyclerView.setAdapter(downlandProgressRecyclerViewAdapter);
mFirstTime = false;
}
}


@Override
protected void onPostExecute(DownloadInfoUpdate diffResult) {
if (!mFirstTime) {
if (!(mFirstTime || downlandProgressRecyclerViewAdapter == null)) {
updateAdaperAndDipatchChanges(diffResult);
showAlternativeView(FINISHED_DOWNLOAD_TYPE);
mShowCancelAll = false;
supportInvalidateOptionsMenu();
} else {
showAlternativeView(ZERO_DOWNLOAD_TYPE);
mFirstTime = false;
Expand All @@ -271,7 +296,9 @@ protected void onCancelled() {
}

private void updateAdaperAndDipatchChanges(DownloadInfoUpdate newItems) {
bookmarkRecyclerViewAdapter.updateItems(newItems);
if (downlandProgressRecyclerViewAdapter != null) {
downlandProgressRecyclerViewAdapter.updateItems(newItems);
}
}
}

Expand Down

0 comments on commit 8fa8d5e

Please sign in to comment.