Skip to content

Commit

Permalink
Merge pull request #92 from AEFeinstein/links
Browse files Browse the repository at this point in the history
Links
  • Loading branch information
AEFeinstein committed Apr 23, 2015
2 parents 17fa35b + 48f2f0f commit 8f8be78
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 207 deletions.
1 change: 1 addition & 0 deletions MTGFamiliar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ dependencies {
compile("com.doomonafireball.betterpickers:library:1.5.3") {
exclude group: 'com.android.support', module: 'support-v4'
}
compile 'com.google.android.gms:play-services-appindexing:7.0.0'
}
31 changes: 28 additions & 3 deletions MTGFamiliar/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gelakinetic.mtgfam"
android:versionCode="32"
android:versionName="3.2.3">
android:versionCode="33"
android:versionName="3.2.4">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand All @@ -15,7 +15,8 @@
android:theme="@style/Theme.light">
<activity
android:name="com.gelakinetic.mtgfam.FamiliarActivity"
android:label="@string/app_name">
android:label="@string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -25,6 +26,26 @@
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<intent-filter android:label="@string/filter_title_view_card">
<action android:name="android.intent.action.VIEW" />
<!-- Accepts URIs that begin with "android-app://com.gelakinetic.mtgfam/card" -->
<data android:scheme="android-app"
android:host="com.gelakinetic.mtgfam"
android:pathPrefix="/card" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

<intent-filter android:label="@string/filter_title_view_card">
<action android:name="android.intent.action.VIEW" />
<!-- Accepts URIs that begin with "android-app://com.gelakinetic.mtgfam/" -->
<data android:scheme="android-app"
android:host="com.gelakinetic.mtgfam"
android:pathPrefix="/" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
Expand Down Expand Up @@ -84,6 +105,10 @@
android:resource="@xml/mtgfamiliar_appwidget_info_light" />
</receiver>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

</application>

</manifest>
342 changes: 219 additions & 123 deletions MTGFamiliar/src/main/java/com/gelakinetic/mtgfam/FamiliarActivity.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
Expand Down Expand Up @@ -68,6 +69,9 @@
import com.gelakinetic.mtgfam.helpers.database.DatabaseManager;
import com.gelakinetic.mtgfam.helpers.database.FamiliarDbException;
import com.gelakinetic.mtgfam.helpers.lruCache.RecyclingBitmapDrawable;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;
Expand Down Expand Up @@ -151,6 +155,12 @@ public class CardViewFragment extends FamiliarFragment {
/* Easier than calling getActivity() all the time, and handles being nested */
private FamiliarActivity mActivity;

/* State for reporting page views */
private boolean mHasReportedView = false;
private boolean mShouldReportView = false;
private String mDescription;
private String mSetName;

/**
* Kill any AsyncTask if it is still running
*/
Expand All @@ -176,6 +186,95 @@ public void onPause() {
}
}

/**
* Called when the fragment stops, attempt to report the close
*/
@Override
public void onStop() {
reportAppIndexEndIfAble();
super.onStop();
}

/**
* Creates and returns the action describing this page view
*
* @return An action describing this page view
*/
Action getAppIndexAction() {

Thing object = new Thing.Builder()
.setType("http://schema.org/Thing") /* Optional, any valid schema.org type */
.setName(mCardName + " (" + mSetName + ")") /* Required, title field */
.setDescription(mDescription) /* Required, description field */
/* Required, deep link in the android-app:// format */
.setUrl(Uri.parse("android-app://com.gelakinetic.mtgfam/card/multiverseid/" + mMultiverseId))
.build();

return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.build();
}

/**
* Reports this view to the Google app indexing API, once, when the fragment is viewed
*/
private void reportAppIndexViewIfAble() {
/* If this view hasn't been reported yet, and the name exists */
if (!mHasReportedView) {
if (mCardName != null) {
/* Connect your client */
getFamiliarActivity().mGoogleApiClient.connect();
AppIndex.AppIndexApi.start(getFamiliarActivity().mGoogleApiClient, getAppIndexAction());

/* Manage state */
mHasReportedView = true;
mShouldReportView = false;
} else {
mShouldReportView = true;
}
}
}

/**
* Ends the report to the Google app indexing API, once, when the fragment is no longer viewed
*/
private void reportAppIndexEndIfAble() {
/* If the view was previously reported, and the name exists */
if (mHasReportedView && mCardName != null) {
/* Call end() and disconnect the client */
AppIndex.AppIndexApi.end(getFamiliarActivity().mGoogleApiClient, getAppIndexAction());
getFamiliarActivity().mGoogleApiClient.disconnect();

/* manage state */
mHasReportedView = false;
}
}

/**
* Set a hint to the system about whether this fragment's UI is currently visible to the user.
* This hint defaults to true and is persistent across fragment instance state save and restore.
* <p/>
* An app may set this to false to indicate that the fragment's UI is scrolled out of visibility
* or is otherwise not directly visible to the user. This may be used by the system to
* prioritize operations such as fragment lifecycle updates or loader ordering behavior.
* <p/>
* In this case, it's used to report fragment views to Google app indexing
*
* @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
* false if it is not.
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
/* If the fragment is visible to the user, attempt to report the view */
reportAppIndexViewIfAble();
} else {
/* The view isn't visible anymore, attempt to report it */
reportAppIndexEndIfAble();
}
}

/**
* Inflates the view and saves references to UI elements for later filling
*
Expand Down Expand Up @@ -267,7 +366,7 @@ private void setInfoFromBundle(Bundle extras) {
long cardID = extras.getLong(CARD_ID);

try {
/* from onCreateView */
/* from onCreateView */
setInfoFromID(cardID);
} catch (FamiliarDbException e) {
handleFamiliarDbException(true);
Expand All @@ -285,13 +384,28 @@ private void setInfoFromID(final long id) throws FamiliarDbException {

ImageGetter imgGetter = ImageGetterHelper.GlyphGetter(getActivity());

while (DatabaseManager.getInstance().mOpenCounter.get() > 0) {
/* Database is busy, updating probably. Spin for a bit
* This happens when a deep link is opened for the first time
* The transactional update collides with fetching card data
*/
}
SQLiteDatabase database = DatabaseManager.getInstance().openDatabase(false);
Cursor cCardById = CardDbAdapter.fetchCard(id, database);

/* http://magiccards.info/scans/en/mt/55.jpg */
mCardName = cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_NAME));
mSetCode = cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_SET));

/* Start building a description */
addToDescription(getString(R.string.search_name), mCardName);
try {
mSetName = CardDbAdapter.getSetNameFromCode(mSetCode, database);
addToDescription(getString(R.string.search_set), mSetName);
} catch (FamiliarDbException e) {
/* no set for you */
}

mMagicCardsInfoSetCode =
CardDbAdapter.getCodeMtgi(cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_SET)),
database);
Expand All @@ -301,34 +415,42 @@ private void setInfoFromID(final long id) throws FamiliarDbException {
case 'C':
case 'c':
mSetTextView.setTextColor(CardViewFragment.this.getResources().getColor(getResourceIdFromAttr(R.attr.color_common)));
addToDescription(getString(R.string.search_rarity), getString(R.string.search_Common));
break;
case 'U':
case 'u':
mSetTextView.setTextColor(CardViewFragment.this.getResources().getColor(getResourceIdFromAttr(R.attr.color_uncommon)));
addToDescription(getString(R.string.search_rarity), getString(R.string.search_Uncommon));
break;
case 'R':
case 'r':
mSetTextView.setTextColor(CardViewFragment.this.getResources().getColor(getResourceIdFromAttr(R.attr.color_rare)));
addToDescription(getString(R.string.search_rarity), getString(R.string.search_Rare));
break;
case 'M':
case 'm':
mSetTextView.setTextColor(CardViewFragment.this.getResources().getColor(getResourceIdFromAttr(R.attr.color_mythic)));
addToDescription(getString(R.string.search_rarity), getString(R.string.search_Mythic));
break;
case 'T':
case 't':
mSetTextView.setTextColor(CardViewFragment.this.getResources().getColor(getResourceIdFromAttr(R.attr.color_timeshifted)));
addToDescription(getString(R.string.search_rarity), getString(R.string.search_Timeshifted));
break;
}

String sCost = cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_MANACOST));
addToDescription(getString(R.string.search_mana_cost), sCost);
CharSequence csCost = ImageGetterHelper.formatStringWithGlyphs(sCost, imgGetter);
mCostTextView.setText(csCost);

String sAbility = cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_ABILITY));
addToDescription(getString(R.string.search_text), sAbility);
CharSequence csAbility = ImageGetterHelper.formatStringWithGlyphs(sAbility, imgGetter);
mAbilityTextView.setText(csAbility);

String sFlavor = cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_FLAVOR));
addToDescription(getString(R.string.search_flavor_text), sFlavor);
CharSequence csFlavor = ImageGetterHelper.formatStringWithGlyphs(sFlavor, imgGetter);
mFlavorTextView.setText(csFlavor);

Expand All @@ -337,6 +459,9 @@ private void setInfoFromID(final long id) throws FamiliarDbException {
mSetTextView.setText(cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_SET)));
mArtistTextView.setText(cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_ARTIST)));

addToDescription(getString(R.string.search_type), cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_TYPE)));
addToDescription(getString(R.string.search_artist), cCardById.getString(cCardById.getColumnIndex(CardDbAdapter.KEY_ARTIST)));

int loyalty = cCardById.getInt(cCardById.getColumnIndex(CardDbAdapter.KEY_LOYALTY));
float p = cCardById.getFloat(cCardById.getColumnIndex(CardDbAdapter.KEY_POWER));
float t = cCardById.getFloat(cCardById.getColumnIndex(CardDbAdapter.KEY_TOUGHNESS));
Expand Down Expand Up @@ -384,6 +509,8 @@ else if (t == CardDbAdapter.STAR_SQUARED)
}
}

addToDescription(getString(R.string.search_power), powTouStr);

mPowTouTextView.setText(powTouStr);
} else {
mPowTouTextView.setText("");
Expand Down Expand Up @@ -464,7 +591,7 @@ public void onClick(View v) {
cCardById.close();

/* Find the other sets this card is in ahead of time, so that it can be remove from the menu if there is only
one set */
one set */
Cursor cCardByName = CardDbAdapter.fetchCardByName(mCardName,
new String[]{
CardDbAdapter.DATABASE_TABLE_CARDS + "." + CardDbAdapter.KEY_SET,
Expand All @@ -485,6 +612,24 @@ public void onClick(View v) {
mActivity.supportInvalidateOptionsMenu();
}
DatabaseManager.getInstance().closeDatabase();

if (mShouldReportView) {
reportAppIndexViewIfAble();
}
}

/**
* Used to build a meta description of this card, for app indexing
*
* @param tag A tag for this data
* @param data The data to add to the description
*/
private void addToDescription(String tag, String data) {
if (mDescription == null) {
mDescription = tag + ": \"" + data + "\"";
} else {
mDescription += "\n" + tag + ": \"" + data + "\"";
}
}

/**
Expand Down Expand Up @@ -1105,10 +1250,10 @@ protected Void doInBackground(Void... params) {

final String imageKey = Integer.toString(mMultiverseId) + cardLanguage;

// Check disk cache in background thread
/* Check disk cache in background thread */
Bitmap bitmap = getFamiliarActivity().mImageCache.getBitmapFromDiskCache(imageKey);

if (bitmap == null) { // Not found in disk cache
if (bitmap == null) { /* Not found in disk cache */

boolean bRetry = true;

Expand All @@ -1127,7 +1272,7 @@ protected Void doInBackground(Void... params) {
u = new URL(getMtgiPicUrl(mCardName, mMagicCardsInfoSetCode, mCardNumber, cardLanguage));
cardLanguage = "en";
} else {
if (!triedMtgi) {
if (!triedMtgi) {
u = new URL(getMtgiPicUrl(mCardName, mMagicCardsInfoSetCode, mCardNumber, cardLanguage));
triedMtgi = true;
} else {
Expand Down Expand Up @@ -1191,7 +1336,7 @@ protected Void doInBackground(Void... params) {
int newWidth = Math.round(bitmap.getWidth() * scale);
int newHeight = Math.round(bitmap.getHeight() * scale);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); // todo this is leaky?
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); /* todo this is leaky? */
mCardBitmap = new RecyclingBitmapDrawable(mActivity.getResources(), scaledBitmap);
bitmap.recycle();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gelakinetic.mtgfam.FamiliarActivity;
import com.gelakinetic.mtgfam.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.RejectedExecutionException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public void onCreate(Bundle savedInstanceState) {
}
}
}
DatabaseManager.getInstance().closeDatabase();

} catch (FamiliarDbException e) {
handleFamiliarDbException(true);
}
Expand All @@ -179,7 +181,6 @@ public void onDestroy() {
if (mCursor != null) {
mCursor.close();
}
DatabaseManager.getInstance().closeDatabase();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import android.widget.AutoCompleteTextView;
import android.widget.SimpleCursorAdapter;

import com.gelakinetic.mtgfam.R;
import com.gelakinetic.mtgfam.fragments.FamiliarFragment;
import com.gelakinetic.mtgfam.helpers.database.CardDbAdapter;
import com.gelakinetic.mtgfam.helpers.database.CardSearchProvider;
Expand Down
Loading

0 comments on commit 8f8be78

Please sign in to comment.