From df05605f033c20fdb9bdc34f359708b82754d32e Mon Sep 17 00:00:00 2001 From: steckbrief Date: Mon, 5 Oct 2015 13:00:21 +0200 Subject: Fixes FS#70 - Introduction of a ConversationsPlusApplication to get global access to app information --- .../ConversationsPlusApplication.java | 62 ++++++++++++++++++++++ .../generator/AbstractGenerator.java | 17 +----- .../conversationsplus/generator/IqGenerator.java | 8 +-- .../services/NotificationService.java | 3 +- .../conversationsplus/ui/AboutPreference.java | 4 +- .../conversationsplus/utils/PhoneHelper.java | 15 +----- 6 files changed, 74 insertions(+), 35 deletions(-) create mode 100644 src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java (limited to 'src/main/java/de/thedevstack') diff --git a/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java b/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java new file mode 100644 index 00000000..dfe67cb7 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java @@ -0,0 +1,62 @@ +package de.thedevstack.conversationsplus; + +import android.app.Application; +import android.content.Context; +import android.content.pm.PackageManager; + +import java.io.File; + +/** + * This class is used to provide static access to the applicationcontext. + */ +public class ConversationsPlusApplication extends Application { + /** + * Application instance for static access + */ + private static ConversationsPlusApplication instance; + + /** + * Initializes the application and saves its instance. + */ + public void onCreate(){ + super.onCreate(); + ConversationsPlusApplication.instance = this; + } + + /** + * Returns the application's context. + * @return Context the application's context + */ + public static Context getAppContext() { + return ConversationsPlusApplication.instance.getApplicationContext(); + } + + /** + * Returns the application's private data directory. + * @return File the application's private data dir + */ + public static File getPrivateFilesDir() { + return ConversationsPlusApplication.instance.getFilesDir(); + } + + public static String getVersion() { + final String packageName = ConversationsPlusApplication.getAppContext().getPackageName(); + if (packageName != null) { + try { + return ConversationsPlusApplication.getAppContext().getPackageManager().getPackageInfo(packageName, 0).versionName; + } catch (final PackageManager.NameNotFoundException e) { + return "unknown"; + } + } else { + return "unknown"; + } + } + + public static String getName() { + return ConversationsPlusApplication.getAppContext().getString(R.string.app_name); + } + + public static String getNameAndVersion() { + return getName() + " " + getVersion(); + } +} diff --git a/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java b/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java index 6737f646..3f4bb839 100644 --- a/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java +++ b/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java @@ -12,9 +12,9 @@ import java.util.List; import java.util.Locale; import java.util.TimeZone; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.services.XmppConnectionService; -import de.thedevstack.conversationsplus.utils.PhoneHelper; public abstract class AbstractGenerator { private final String[] FEATURES = { @@ -35,8 +35,6 @@ public abstract class AbstractGenerator { "urn:xmpp:receipts" }; private String mVersion = null; - //FIXME use appname or similar!!!!!!!!!!!11elf - public final String IDENTITY_NAME = "Conversations+"; public final String IDENTITY_TYPE = "phone"; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); @@ -47,20 +45,9 @@ public abstract class AbstractGenerator { this.mXmppConnectionService = service; } - protected String getIdentityVersion() { - if (mVersion == null) { - this.mVersion = PhoneHelper.getVersionName(mXmppConnectionService); - } - return this.mVersion; - } - - protected String getIdentityName() { - return IDENTITY_NAME + " " + getIdentityVersion(); - } - public String getCapHash() { StringBuilder s = new StringBuilder(); - s.append("client/" + IDENTITY_TYPE + "//" + getIdentityName() + "<"); + s.append("client/" + IDENTITY_TYPE + "//" + ConversationsPlusApplication.getNameAndVersion() + "<"); MessageDigest md; try { md = MessageDigest.getInstance("SHA-1"); diff --git a/src/main/java/de/thedevstack/conversationsplus/generator/IqGenerator.java b/src/main/java/de/thedevstack/conversationsplus/generator/IqGenerator.java index 8199e59c..3382ee23 100644 --- a/src/main/java/de/thedevstack/conversationsplus/generator/IqGenerator.java +++ b/src/main/java/de/thedevstack/conversationsplus/generator/IqGenerator.java @@ -4,12 +4,12 @@ package de.thedevstack.conversationsplus.generator; import java.util.ArrayList; import java.util.List; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.DownloadableFile; import de.thedevstack.conversationsplus.services.MessageArchiveService; import de.thedevstack.conversationsplus.services.XmppConnectionService; -import de.thedevstack.conversationsplus.utils.PhoneHelper; import de.thedevstack.conversationsplus.utils.Xmlns; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.forms.Data; @@ -33,7 +33,7 @@ public class IqGenerator extends AbstractGenerator { final Element identity = query.addChild("identity"); identity.setAttribute("category", "client"); identity.setAttribute("type", IDENTITY_TYPE); - identity.setAttribute("name", getIdentityName()); + identity.setAttribute("name", ConversationsPlusApplication.getNameAndVersion()); for (final String feature : getFeatures()) { query.addChild("feature").setAttribute("var", feature); } @@ -43,8 +43,8 @@ public class IqGenerator extends AbstractGenerator { public IqPacket versionResponse(final IqPacket request) { final IqPacket packet = request.generateResponse(IqPacket.TYPE.RESULT); Element query = packet.query("jabber:iq:version"); - query.addChild("name").setContent(IDENTITY_NAME); - query.addChild("version").setContent(getIdentityVersion()); + query.addChild("name").setContent(ConversationsPlusApplication.getName()); + query.addChild("version").setContent(ConversationsPlusApplication.getVersion()); return packet; } diff --git a/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java b/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java index 563a7d67..e6692d40 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.R; @@ -82,7 +83,7 @@ public class NotificationService { final String notificationData = new JSONArray().put(jsonData).toString(); i.putExtra("messageType", "PEBBLE_ALERT"); - i.putExtra("sender", "Conversations"); /* XXX: Shouldn't be hardcoded, e.g., AbstractGenerator.APP_NAME); */ + i.putExtra("sender", ConversationsPlusApplication.getName()); i.putExtra("notificationData", notificationData); // notify Pebble App i.setPackage("com.getpebble.android"); diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/AboutPreference.java b/src/main/java/de/thedevstack/conversationsplus/ui/AboutPreference.java index c1fd4d67..8a02b680 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/AboutPreference.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/AboutPreference.java @@ -5,7 +5,7 @@ import android.content.Intent; import android.preference.Preference; import android.util.AttributeSet; -import de.thedevstack.conversationsplus.utils.PhoneHelper; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; public class AboutPreference extends Preference { public AboutPreference(final Context context, final AttributeSet attrs, final int defStyle) { @@ -26,7 +26,7 @@ public class AboutPreference extends Preference { } private void setSummary() { - setSummary("Conversations+ " + PhoneHelper.getVersionName(getContext())); + setSummary(ConversationsPlusApplication.getNameAndVersion()); } } diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/PhoneHelper.java b/src/main/java/de/thedevstack/conversationsplus/utils/PhoneHelper.java index b49e2183..d1f598c0 100644 --- a/src/main/java/de/thedevstack/conversationsplus/utils/PhoneHelper.java +++ b/src/main/java/de/thedevstack/conversationsplus/utils/PhoneHelper.java @@ -14,6 +14,8 @@ import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.Profile; +import de.thedevstack.conversationsplus.ConversationsPlusApplication; + public class PhoneHelper { public static void loadPhoneContacts(Context context,final List phoneContacts, final OnPhoneContactsLoadedListener listener) { @@ -91,17 +93,4 @@ public class PhoneHelper { } } } - - public static String getVersionName(Context context) { - final String packageName = context == null ? null : context.getPackageName(); - if (packageName != null) { - try { - return context.getPackageManager().getPackageInfo(packageName, 0).versionName; - } catch (final PackageManager.NameNotFoundException e) { - return "unknown"; - } - } else { - return "unknown"; - } - } } -- cgit v1.2.3