mirror of
https://github.com/ethauvin/android-about-box.git
synced 2025-04-25 02:37:11 -07:00
Merge pull request #25 from eggheadgames/feature/null_check_to_protect_from_crash
Fix "Force close on launching AboutActivity"
This commit is contained in:
commit
ddca85d1b3
2 changed files with 243 additions and 222 deletions
|
@ -0,0 +1,97 @@
|
|||
package com.eggheadgames.aboutbox;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.widget.Toast;
|
||||
|
||||
public final class AboutBoxUtils {
|
||||
|
||||
private AboutBoxUtils() {
|
||||
//nothing
|
||||
}
|
||||
|
||||
public static void getOpenFacebookIntent(Activity context, String name) {
|
||||
try {
|
||||
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + name));
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + name));
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e1) {
|
||||
Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void startTwitter(Activity context, String name) {
|
||||
try {
|
||||
context.getPackageManager().getPackageInfo("com.twitter.android", 0);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + name));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/" + name));
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e1) {
|
||||
Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void openApp(Activity context, AboutConfig.BuildType buildType, String packageName) {
|
||||
String appURI = null;
|
||||
String webURI = null;
|
||||
switch (buildType) {
|
||||
case GOOGLE:
|
||||
appURI = "market://details?id=" + packageName;
|
||||
webURI = "http://play.google.com/store/apps/details?id=" + packageName;
|
||||
break;
|
||||
case AMAZON:
|
||||
appURI = "amzn://apps/android?p=" + packageName;
|
||||
webURI = "http://www.amazon.com/gp/mas/dl/android?p=" + packageName;
|
||||
break;
|
||||
default:
|
||||
//nothing
|
||||
}
|
||||
openApplication(context, appURI, webURI);
|
||||
}
|
||||
|
||||
public static void openPublisher(Activity context, AboutConfig.BuildType buildType, String publisher, String packageName) {
|
||||
String appURI = null;
|
||||
String webURI = null;
|
||||
switch (buildType) {
|
||||
case GOOGLE:
|
||||
appURI = "market://search?q=pub:" + publisher;
|
||||
webURI = "http://play.google.com/store/search?q=pub:" + publisher;
|
||||
break;
|
||||
case AMAZON:
|
||||
appURI = "amzn://apps/android?showAll=1&p=" + packageName;
|
||||
webURI = "http://www.amazon.com/gp/mas/dl/android?showAll=1&p=" + packageName;
|
||||
break;
|
||||
default:
|
||||
//nothing
|
||||
}
|
||||
openApplication(context, appURI, webURI);
|
||||
}
|
||||
|
||||
public static void openApplication(Activity context, String appURI, String webURI) {
|
||||
try {
|
||||
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(appURI)));
|
||||
} catch (ActivityNotFoundException e1) {
|
||||
try {
|
||||
openHTMLPage(context, webURI);
|
||||
} catch (ActivityNotFoundException e2) {
|
||||
Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void openHTMLPage(Activity context, String htmlPath) {
|
||||
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(htmlPath)));
|
||||
}
|
||||
}
|
|
@ -1,13 +1,10 @@
|
|||
package com.eggheadgames.aboutbox.activity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.danielstone.materialaboutlibrary.MaterialAboutActivity;
|
||||
import com.danielstone.materialaboutlibrary.items.MaterialAboutActionItem;
|
||||
|
@ -15,6 +12,7 @@ import com.danielstone.materialaboutlibrary.items.MaterialAboutItemOnClickListen
|
|||
import com.danielstone.materialaboutlibrary.items.MaterialAboutTitleItem;
|
||||
import com.danielstone.materialaboutlibrary.model.MaterialAboutCard;
|
||||
import com.danielstone.materialaboutlibrary.model.MaterialAboutList;
|
||||
import com.eggheadgames.aboutbox.AboutBoxUtils;
|
||||
import com.eggheadgames.aboutbox.AboutConfig;
|
||||
import com.eggheadgames.aboutbox.IAnalytic;
|
||||
import com.eggheadgames.aboutbox.R;
|
||||
|
@ -69,7 +67,7 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
@Override
|
||||
public void onClick(boolean b) {
|
||||
if (config.dialog == null) {
|
||||
openHTMLPage(config.guideHtmlPath);
|
||||
AboutBoxUtils.openHTMLPage(AboutActivity.this, config.guideHtmlPath);
|
||||
} else {
|
||||
config.dialog.open(AboutActivity.this, config.guideHtmlPath, getString(R.string.egab_guide));
|
||||
}
|
||||
|
@ -93,16 +91,16 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
@NonNull
|
||||
private MaterialAboutCard buildShareCard(final AboutConfig config) {
|
||||
MaterialAboutCard.Builder card = new MaterialAboutCard.Builder();
|
||||
|
||||
if (config.buildType != null && !TextUtils.isEmpty(config.packageName)) {
|
||||
card.addItem(itemHelper(R.string.egab_leave_review, R.drawable.ic_review,
|
||||
new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
openApp(config.buildType, config.packageName);
|
||||
AboutBoxUtils.openApp(AboutActivity.this, config.buildType, config.packageName);
|
||||
logUIEventName(config.analytics, config.logUiEventName, getString(R.string.egab_review_log_event));
|
||||
}
|
||||
}));
|
||||
|
||||
}
|
||||
card.addItem(itemHelper(R.string.egab_share, R.drawable.ic_share_black,
|
||||
new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
|
@ -118,15 +116,18 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
@NonNull
|
||||
private MaterialAboutCard buildAboutCard(final AboutConfig config) {
|
||||
MaterialAboutCard.Builder card = new MaterialAboutCard.Builder();
|
||||
|
||||
if (config.buildType != null && !TextUtils.isEmpty(config.appPublisher) && !TextUtils.isEmpty(config.packageName)) {
|
||||
card.addItem(itemHelper(R.string.egab_try_other_apps, R.drawable.ic_try_other_apps,
|
||||
new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
openPublisher(config.buildType, config.appPublisher, config.packageName);
|
||||
AboutBoxUtils.openPublisher(AboutActivity.this, config.buildType,
|
||||
config.appPublisher, config.packageName);
|
||||
logUIEventName(config.analytics, config.logUiEventName, getString(R.string.egab_try_other_app_log_event));
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (!TextUtils.isEmpty(config.companyHtmlPath)) {
|
||||
card.addItem(new MaterialAboutActionItem.Builder()
|
||||
.text(config.aboutLabelTitle)
|
||||
.icon(R.drawable.ic_about_black)
|
||||
|
@ -134,7 +135,7 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
@Override
|
||||
public void onClick(boolean b) {
|
||||
if (config.dialog == null) {
|
||||
openHTMLPage(config.companyHtmlPath);
|
||||
AboutBoxUtils.openHTMLPage(AboutActivity.this, config.companyHtmlPath);
|
||||
} else {
|
||||
config.dialog.open(AboutActivity.this, config.companyHtmlPath, config.aboutLabelTitle);
|
||||
}
|
||||
|
@ -142,14 +143,14 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
}
|
||||
})
|
||||
.build());
|
||||
|
||||
}
|
||||
return card.build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private MaterialAboutCard buildSocialNetworksCard(final AboutConfig config) {
|
||||
MaterialAboutCard.Builder card = new MaterialAboutCard.Builder();
|
||||
|
||||
if (!TextUtils.isEmpty(config.facebookUserName)) {
|
||||
card.addItem(new MaterialAboutActionItem.Builder()
|
||||
.text(R.string.egab_facebook_label)
|
||||
.subText(config.facebookUserName)
|
||||
|
@ -157,11 +158,13 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
.setOnClickListener(new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
getOpenFacebookIntent(AboutActivity.this, config.facebookUserName);
|
||||
AboutBoxUtils.getOpenFacebookIntent(AboutActivity.this, config.facebookUserName);
|
||||
logUIEventName(config.analytics, config.logUiEventName, getString(R.string.egab_facebook_log_event));
|
||||
}
|
||||
})
|
||||
.build());
|
||||
}
|
||||
if (!TextUtils.isEmpty(config.twitterUserName)) {
|
||||
card.addItem(new MaterialAboutActionItem.Builder()
|
||||
.text(R.string.egab_twitter_label)
|
||||
.subText(config.twitterUserName)
|
||||
|
@ -169,12 +172,13 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
.setOnClickListener(new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
startTwitter(AboutActivity.this, config.twitterUserName);
|
||||
AboutBoxUtils.startTwitter(AboutActivity.this, config.twitterUserName);
|
||||
logUIEventName(config.analytics, config.logUiEventName, getString(R.string.egab_twitter_log_event));
|
||||
}
|
||||
})
|
||||
.build());
|
||||
|
||||
}
|
||||
if (!TextUtils.isEmpty(config.webHomePage)) {
|
||||
card.addItem(new MaterialAboutActionItem.Builder()
|
||||
.text(R.string.egab_web_label)
|
||||
.subText(config.webHomePage.replace("https://", "").replace("http://", "").replace("/", ""))
|
||||
|
@ -182,25 +186,25 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
.setOnClickListener(new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
openHTMLPage(config.webHomePage);
|
||||
AboutBoxUtils.openHTMLPage(AboutActivity.this, config.webHomePage);
|
||||
logUIEventName(config.analytics, config.logUiEventName, getString(R.string.egab_website_log_event));
|
||||
}
|
||||
})
|
||||
.build());
|
||||
|
||||
}
|
||||
return card.build();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private MaterialAboutCard buildPrivacyCard(final AboutConfig config) {
|
||||
MaterialAboutCard.Builder card = new MaterialAboutCard.Builder();
|
||||
|
||||
if (!TextUtils.isEmpty(config.privacyHtmlPath)) {
|
||||
card.addItem(itemHelper(R.string.egab_privacy_policy, R.drawable.ic_privacy,
|
||||
new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
if (config.dialog == null) {
|
||||
openHTMLPage(config.privacyHtmlPath);
|
||||
AboutBoxUtils.openHTMLPage(AboutActivity.this, config.privacyHtmlPath);
|
||||
} else {
|
||||
config.dialog.open(AboutActivity.this, config.privacyHtmlPath, getString(R.string.egab_privacy_policy));
|
||||
}
|
||||
|
@ -209,12 +213,14 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
if (!TextUtils.isEmpty(config.acknowledgmentHtmlPath)) {
|
||||
card.addItem(itemHelper(R.string.egab_acknowledgements, R.drawable.ic_acknowledgements,
|
||||
new MaterialAboutItemOnClickListener() {
|
||||
@Override
|
||||
public void onClick(boolean b) {
|
||||
if (config.dialog == null) {
|
||||
openHTMLPage(config.acknowledgmentHtmlPath);
|
||||
AboutBoxUtils.openHTMLPage(AboutActivity.this, config.acknowledgmentHtmlPath);
|
||||
} else {
|
||||
config.dialog.open(AboutActivity.this, config.acknowledgmentHtmlPath, getString(R.string.egab_acknowledgements));
|
||||
}
|
||||
|
@ -222,6 +228,7 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
return card.build();
|
||||
}
|
||||
|
||||
|
@ -239,89 +246,6 @@ public class AboutActivity extends MaterialAboutActivity {
|
|||
return getString(R.string.egab_about_screen_title);
|
||||
}
|
||||
|
||||
public static void getOpenFacebookIntent(Activity context, String name) {
|
||||
try {
|
||||
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + name));
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + name));
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e1) {
|
||||
Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void startTwitter(Activity context, String name) {
|
||||
try {
|
||||
context.getPackageManager().getPackageInfo("com.twitter.android", 0);
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + name));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/" + name));
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e1) {
|
||||
Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void openApp(AboutConfig.BuildType buildType, String packageName) {
|
||||
String appURI = null;
|
||||
String webURI = null;
|
||||
switch (buildType) {
|
||||
case GOOGLE:
|
||||
appURI = "market://details?id=" + packageName;
|
||||
webURI = "http://play.google.com/store/apps/details?id=" + packageName;
|
||||
break;
|
||||
case AMAZON:
|
||||
appURI = "amzn://apps/android?p=" + packageName;
|
||||
webURI = "http://www.amazon.com/gp/mas/dl/android?p=" + packageName;
|
||||
break;
|
||||
default:
|
||||
//nothing
|
||||
}
|
||||
openApplication(appURI, webURI);
|
||||
}
|
||||
|
||||
public void openPublisher(AboutConfig.BuildType buildType, String publisher, String packageName) {
|
||||
String appURI = null;
|
||||
String webURI = null;
|
||||
switch (buildType) {
|
||||
case GOOGLE:
|
||||
appURI = "market://search?q=pub:" + publisher;
|
||||
webURI = "http://play.google.com/store/search?q=pub:" + publisher;
|
||||
break;
|
||||
case AMAZON:
|
||||
appURI = "amzn://apps/android?showAll=1&p=" + packageName;
|
||||
webURI = "http://www.amazon.com/gp/mas/dl/android?showAll=1&p=" + packageName;
|
||||
break;
|
||||
default:
|
||||
//nothing
|
||||
}
|
||||
openApplication(appURI, webURI);
|
||||
}
|
||||
|
||||
private void openApplication(String appURI, String webURI) {
|
||||
try {
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(appURI)));
|
||||
} catch (ActivityNotFoundException e1) {
|
||||
try {
|
||||
openHTMLPage(webURI);
|
||||
} catch (ActivityNotFoundException e2) {
|
||||
Toast.makeText(this, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void openHTMLPage(String htmlPath) {
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(htmlPath)));
|
||||
}
|
||||
|
||||
private void logUIEventName(IAnalytic analytics, String eventType, String eventValue) {
|
||||
if (analytics != null) {
|
||||
analytics.logUiEvent(eventType, eventValue);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue