diff --git a/QKSMS/build.gradle b/QKSMS/build.gradle index b6ece62a..241209fe 100644 --- a/QKSMS/build.gradle +++ b/QKSMS/build.gradle @@ -55,5 +55,4 @@ dependencies { // External services compile 'com.crittercism:crittercism-android-agent:5.0.3' - compile files('libs/endlessjabbersdk.jar') } diff --git a/QKSMS/libs/endlessjabbersdk.jar b/QKSMS/libs/endlessjabbersdk.jar deleted file mode 100644 index 55a014e7..00000000 Binary files a/QKSMS/libs/endlessjabbersdk.jar and /dev/null differ diff --git a/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberInterface.java b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberInterface.java new file mode 100644 index 00000000..df4c5c26 --- /dev/null +++ b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberInterface.java @@ -0,0 +1,105 @@ +package com.mariussoft.endlessjabber.sdk; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.net.Uri; + +public class EndlessJabberInterface { + + /** Checks whether or not EndlessJabber is installed */ + public static boolean IsInstalled(Context ctx) { + + PackageManager pm = ctx.getPackageManager(); + try { + pm.getPackageInfo("com.mariussoft.endlessjabber", PackageManager.GET_ACTIVITIES); + return true; + } catch (NameNotFoundException e) { + return false; + } + } + + /** Opens up GooglePlay and redirects it to the EndlessJabber app */ + public static void OpenGooglePlayLink(Context ctx) { + final String appPackageName = "com.mariussoft.endlessjabber"; + try { + ctx.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); + } catch (android.content.ActivityNotFoundException anfe) { + ctx.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName))); + } + } + + /** Launches EndlessJabber */ + public static void LaunchEndlessJabber(Context ctx) { + Intent intent = ctx.getPackageManager().getLaunchIntentForPackage("com.mariussoft.endlessjabber"); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + ctx.startActivity(intent); + } + + /** + * Enables integration by specifying which class to call + * + * @param context + * The context to use + * @param concreteImplementation + * The concrete implementation to call on events + * @param sendSMS + * If true, your app will be responsible for sending SMS messages + * as well as presisting them to the SMS repository + * @param sendMMS + * If true, your app will be responsible for sending & persisting + * MMS messages to the MMS repository + * @throws Exception + */ + @SuppressWarnings("rawtypes") + public static void EnableIntegration(Context context, Class concreteImplementation, boolean sendSMS, boolean sendMMS) throws Exception { + Class clazz = concreteImplementation; + if (!IEndlessJabberImplementation.class.isAssignableFrom(clazz)) { + throw new Exception("Class must implement IEndlessJabberImplementation"); + } + + SharedPreferences.Editor editor = context.getSharedPreferences("EndlessJabberSDK", Context.MODE_PRIVATE).edit(); + editor.putString("InterfaceClass", concreteImplementation.getName()); + editor.putBoolean("SendSMS", sendSMS); + editor.putBoolean("SendMMS", sendMMS); + editor.commit(); + + SendInfoToEndlessJabber(context); + } + + /** Disables integration */ + @SuppressWarnings("rawtypes") + public static void DisableIntegration(Context context, Class nameOfImplementation) { + SharedPreferences.Editor editor = context.getSharedPreferences("EndlessJabberSDK", Context.MODE_PRIVATE).edit(); + editor.clear(); + editor.commit(); + + SendInfoToEndlessJabber(context); + } + + /** + * Refresh integration info with EndlessJabber + * + * @param ctx + * Context to use + */ + static void SendInfoToEndlessJabber(Context context) { + String EndlessJabber_INTENT = "com.mariussoft.endlessjabber.action.extendResponse"; + SharedPreferences prefs = context.getSharedPreferences("EndlessJabberSDK", Context.MODE_PRIVATE); + + Intent i = new Intent(); + i.setAction(EndlessJabber_INTENT); + i.putExtra("Action", "UpdateInfo"); + i.putExtra("PackageName", context.getPackageName()); + i.putExtra("Enabled", prefs.contains("InterfaceClass")); + + if (prefs.contains("InterfaceClass")) { + i.putExtra("SendSMS", prefs.getBoolean("SendSMS", false)); + i.putExtra("SendMMS", prefs.getBoolean("SendMMS", false)); + } + + context.sendBroadcast(i); + } +} diff --git a/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberReceiver.java b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberReceiver.java new file mode 100644 index 00000000..9eebc92c --- /dev/null +++ b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberReceiver.java @@ -0,0 +1,22 @@ +package com.mariussoft.endlessjabber.sdk; + +import android.content.Context; +import android.content.Intent; + +public class EndlessJabberReceiver extends WakefulBroadcastReceiver { + + public static final String EndlessJabber_INTENT = "com.mariussoft.endlessjabber.action.extend"; + + @Override + public void onReceive(Context context, Intent intent) { + + if (intent.getAction().equals(EndlessJabber_INTENT)) { + + // This is the Intent to deliver to our service. + Intent service = new Intent(context, EndlessJabberWakefulService.class); + service.putExtras(intent.getExtras()); + + startWakefulService(context, service); + } + } +} diff --git a/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberWakefulService.java b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberWakefulService.java new file mode 100644 index 00000000..dbe22a51 --- /dev/null +++ b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/EndlessJabberWakefulService.java @@ -0,0 +1,78 @@ +package com.mariussoft.endlessjabber.sdk; + +import android.app.IntentService; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +public class EndlessJabberWakefulService extends IntentService { + + public EndlessJabberWakefulService() { + super("EndlessJabberWakefulService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + Context context = getApplicationContext(); + + // Get the class to call + String implemenationClass = context.getSharedPreferences("EndlessJabberSDK", Context.MODE_PRIVATE).getString("InterfaceClass", null); + + if (implemenationClass == null) { + return; + } + + IEndlessJabberImplementation instanceOfMyClass; + try { + instanceOfMyClass = (IEndlessJabberImplementation) Class.forName(implemenationClass).newInstance(); + + Bundle extras = intent.getExtras(); + + switch (extras.getString("Action")) { + case "UpdateRead": { + long time = extras.getLong("Time"); + int conversationID = extras.getInt("ConversationID"); + instanceOfMyClass.UpdateReadMessages(context, time, conversationID); + break; + } + case "DeleteThread": { + + int conversationID = extras.getInt("ConversationID"); + instanceOfMyClass.DeleteThread(context, conversationID); + break; + } + case "SendMMS": { + + boolean save = extras.getBoolean("Save"); + boolean send = extras.getBoolean("Send"); + String[] Recipients = extras.getStringArray("Recipients"); + + MMSPart[] parts = new MMSPart[extras.getParcelableArray("Parts").length]; + + for (int i = 0; i < parts.length; i++) { + parts[i] = (MMSPart) extras.getParcelableArray("Parts")[i]; + } + + instanceOfMyClass.SendMMS(context, Recipients, parts, extras.getString("Subject"), save, send); + break; + } + case "SendSMS": { + boolean send = extras.getBoolean("Send"); + String[] Recipients = extras.getStringArray("Recipients"); + String message = extras.getString("Message"); + + instanceOfMyClass.SendSMS(context, Recipients, message, send); + break; + } + case "UpdateInfo": { + EndlessJabberInterface.SendInfoToEndlessJabber(context); + break; + } + } + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + + } + + EndlessJabberReceiver.completeWakefulIntent(intent); + } +} diff --git a/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/IEndlessJabberImplementation.java b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/IEndlessJabberImplementation.java new file mode 100644 index 00000000..eddd832c --- /dev/null +++ b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/IEndlessJabberImplementation.java @@ -0,0 +1,50 @@ +package com.mariussoft.endlessjabber.sdk; + +import android.content.Context; + +/** + * + * Interface to implement EndlessJabber integration + * + */ +public interface IEndlessJabberImplementation { + + /** + * Update all messages in the SMS/MMS repositories as read where the time <= the provided time as well as where the threadID matches the provided ID + * @param context The context to utilize + * @param time The 'time' in the SMS database to use when marking messages as read + * (NOTE* for MMS divide by 1000 as MMS repository stores times as 1/1000th of the SMS) + * @param threadID The ID of the thread in the SMS/MMS repositories to update + */ + void UpdateReadMessages(Context context, long time, int threadID); + + /** + * Delete any messages/conversations in the SMS/MMS repositories where the threadID matches the provided ID + * @param context The context to utilize + * @param threadID The ID of the thread in the SMS/MMS repositories to delete + */ + void DeleteThread(Context context,int threadID); + + /** + * Method gets called when EndlessJabber is requested to send an MMS message via the web app + * @param context The context to utilize + * @param recipients List of recipients to send message to + * @param parts The message parts to send along with message + * @param subject The MMS subject of the message + * @param save Whether or not to save the MMS to the MMS repository + * @param send If true, send the message along with saving it to the MMS repository + * NOTE: On KitKat save will be true only when enabled in the SDK & your app is the default messaging app on the system + * + * If both save & send are false, this call is only for informational purposes (e.g. modify notifications, update UI, etc...) + */ + void SendMMS(Context context, String[] recipients, MMSPart[] parts, String subject, boolean save, boolean send); + + /** + * Method gets called when EndlessJabber is requested to send an SMS message via the web app + * @param context The context to utilize + * @param recipients List of recipients to send message to + * @param message The message to send + * @param send If true, send the message along with saving it to the SMS repository, otherwise this is only for informational purposes (e.g. modify notifications, update UI, etc...) + */ + void SendSMS(Context context, String[] recipients, String message, boolean send); +} diff --git a/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/MMSPart.java b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/MMSPart.java new file mode 100644 index 00000000..278aa6d5 --- /dev/null +++ b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/MMSPart.java @@ -0,0 +1,57 @@ +package com.mariussoft.endlessjabber.sdk; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * + * An MMS attachment + * + */ +public class MMSPart implements Parcelable { + /** + * The name of the MMS attachment + */ + public String Name = ""; + /** + * The MimeType of the MMS attachment + */ + public String MimeType = ""; + /** + * The byte[] of the MMS attachment + */ + public byte[] Data; + + public MMSPart() { + } + + public MMSPart(Parcel parcel) { + Name = parcel.readString(); + MimeType = parcel.readString(); + Data = new byte[parcel.readInt()]; + parcel.readByteArray(Data); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int arg1) { + dest.writeString(Name); + dest.writeString(MimeType); + dest.writeInt(Data.length); + dest.writeByteArray(Data); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public MMSPart createFromParcel(Parcel in) { + return new MMSPart(in); + } + + public MMSPart[] newArray(int size) { + return new MMSPart[size]; + } + }; +} \ No newline at end of file diff --git a/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/WakefulBroadcastReceiver.java b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/WakefulBroadcastReceiver.java new file mode 100644 index 00000000..d75f50a5 --- /dev/null +++ b/QKSMS/src/main/java/com/mariussoft/endlessjabber/sdk/WakefulBroadcastReceiver.java @@ -0,0 +1,135 @@ +package com.mariussoft.endlessjabber.sdk; + +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.os.PowerManager; +import android.util.Log; +import android.util.SparseArray; + +/** + * Helper for the common pattern of implementing a {@link BroadcastReceiver} + * that receives a device wakeup event and then passes the work off + * to a {@link android.app.Service}, while ensuring that the + * device does not go back to sleep during the transition. + * + *

This class takes care of creating and managing a partial wake lock + * for you; you must request the {@link android.Manifest.permission#WAKE_LOCK} + * permission to use it.

+ * + *

Example

+ * + *

A {@link WakefulBroadcastReceiver} uses the method + * {@link WakefulBroadcastReceiver#startWakefulService startWakefulService()} + * to start the service that does the work. This method is comparable to + * {@link android.content.Context#startService startService()}, except that + * the {@link WakefulBroadcastReceiver} is holding a wake lock when the service + * starts. The intent that is passed with + * {@link WakefulBroadcastReceiver#startWakefulService startWakefulService()} + * holds an extra identifying the wake lock.

+ * + * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulReceiver.java complete} + * + *

The service (in this example, an {@link android.app.IntentService}) does + * some work. When it is finished, it releases the wake lock by calling + * {@link WakefulBroadcastReceiver#completeWakefulIntent + * completeWakefulIntent(intent)}. The intent it passes as a parameter + * is the same intent that the {@link WakefulBroadcastReceiver} originally + * passed in.

+ * + * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulService.java complete} + */ +public abstract class WakefulBroadcastReceiver extends BroadcastReceiver { + private static final String EXTRA_WAKE_LOCK_ID = "android.support.content.wakelockid"; + + private static final SparseArray mActiveWakeLocks + = new SparseArray(); + private static int mNextId = 1; + + /** + * Do a {@link android.content.Context#startService(android.content.Intent) + * Context.startService}, but holding a wake lock while the service starts. + * This will modify the Intent to hold an extra identifying the wake lock; + * when the service receives it in {@link android.app.Service#onStartCommand + * Service.onStartCommand}, it should pass back the Intent it receives there to + * {@link #completeWakefulIntent(android.content.Intent)} in order to release + * the wake lock. + * + * @param context The Context in which it operate. + * @param intent The Intent with which to start the service, as per + * {@link android.content.Context#startService(android.content.Intent) + * Context.startService}. + */ + public static ComponentName startWakefulService(Context context, Intent intent) { + synchronized (mActiveWakeLocks) { + int id = mNextId; + mNextId++; + if (mNextId <= 0) { + mNextId = 1; + } + + intent.putExtra(EXTRA_WAKE_LOCK_ID, id); + ComponentName comp = context.startService(intent); + if (comp == null) { + return null; + } + + PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + "wake:" + comp.flattenToShortString()); + wl.setReferenceCounted(false); + wl.acquire(60*1000); + mActiveWakeLocks.put(id, wl); + return comp; + } + } + + /** + * Finish the execution from a previous {@link #startWakefulService}. Any wake lock + * that was being held will now be released. + * + * @param intent The Intent as originally generated by {@link #startWakefulService}. + * @return Returns true if the intent is associated with a wake lock that is + * now released; returns false if there was no wake lock specified for it. + */ + public static boolean completeWakefulIntent(Intent intent) { + final int id = intent.getIntExtra(EXTRA_WAKE_LOCK_ID, 0); + if (id == 0) { + return false; + } + synchronized (mActiveWakeLocks) { + PowerManager.WakeLock wl = mActiveWakeLocks.get(id); + if (wl != null) { + wl.release(); + mActiveWakeLocks.remove(id); + return true; + } + // We return true whether or not we actually found the wake lock + // the return code is defined to indicate whether the Intent contained + // an identifier for a wake lock that it was supposed to match. + // We just log a warning here if there is no wake lock found, which could + // happen for example if this function is called twice on the same + // intent or the process is killed and restarted before processing the intent. + Log.w("WakefulBroadcastReceiver", "No active wake lock id #" + id); + return true; + } + } +} \ No newline at end of file