|
|
@@ -0,0 +1,128 @@
|
|
|
+package com.knacki.mimou.bridge;
|
|
|
+
|
|
|
+import android.webkit.JavascriptInterface;
|
|
|
+
|
|
|
+import com.knacki.mimou.activity.MainActivity;
|
|
|
+import com.knacki.mimou.contact.Contact;
|
|
|
+import com.knacki.mimou.contact.ContactReader;
|
|
|
+import com.knacki.mimou.preference.CredentialHolder;
|
|
|
+import com.knacki.mimou.sms.Conversation;
|
|
|
+import com.knacki.mimou.sms.ConversationList;
|
|
|
+import com.knacki.mimou.sms.Sms;
|
|
|
+import com.knacki.mimou.sms.SmsReader;
|
|
|
+
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.logging.Level;
|
|
|
+import java.util.logging.Logger;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by thibal on 1/4/18.
|
|
|
+ */
|
|
|
+public class JsInterface {
|
|
|
+ private final Logger log = Logger.getLogger(JsInterface.class.getName());
|
|
|
+ MainActivity mainActivity;
|
|
|
+
|
|
|
+ public void setActivity(MainActivity mainActivity) {
|
|
|
+ this.mainActivity = mainActivity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JavascriptInterface
|
|
|
+ public void logout() {
|
|
|
+ CredentialHolder.removeToken(mainActivity);
|
|
|
+ mainActivity.showLogin();
|
|
|
+ }
|
|
|
+
|
|
|
+ @JavascriptInterface
|
|
|
+ public void readSmsPermission(final String fncName) {
|
|
|
+ mainActivity.requestSmsPermission(new TypedCallback<Boolean>() {
|
|
|
+ @Override
|
|
|
+ public void onResult(Boolean result) {
|
|
|
+ mainActivity.runJavascript(new JavascriptFunction(fncName).addArgument(result));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @JavascriptInterface
|
|
|
+ public String getSms() {
|
|
|
+ List<Sms> smsList = SmsReader.readAll(mainActivity);
|
|
|
+ JSONArray smsArray = new JSONArray();
|
|
|
+
|
|
|
+ for (Sms sms: smsList) {
|
|
|
+ try {
|
|
|
+ smsArray.put(sms.toJSON());
|
|
|
+ } catch (JSONException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot stringify message", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return smsArray.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @JavascriptInterface
|
|
|
+ public String getStatic() {
|
|
|
+ JSONObject staticContent = new JSONObject();
|
|
|
+
|
|
|
+ getStaticChannels(staticContent);
|
|
|
+ getStaticUsers(staticContent);
|
|
|
+
|
|
|
+ JSONObject wrapper = new JSONObject();
|
|
|
+ try {
|
|
|
+ wrapper.put("static", staticContent);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot JSONify static content", e);
|
|
|
+ }
|
|
|
+ return wrapper.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getStaticUsers(JSONObject staticContent) {
|
|
|
+ JSONArray jsonUsers = new JSONArray();
|
|
|
+ JSONObject genericContent = new JSONObject();
|
|
|
+
|
|
|
+ for (Contact i: ContactReader.getAllContacts(mainActivity)) {
|
|
|
+ try {
|
|
|
+ jsonUsers.put(i.toJSON());
|
|
|
+ } catch (JSONException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot stringify contact " +i.getName(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ genericContent.put("users", jsonUsers);
|
|
|
+ staticContent.put("SIM|generic", genericContent);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ log.severe("Cannot stringify static users");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getStaticChannels(JSONObject staticContent) {
|
|
|
+ for (Map.Entry<Integer, ConversationList> conversationBySimcard: SmsReader.getAllConversations(mainActivity).entrySet()) {
|
|
|
+ JSONObject simConversations = new JSONObject();
|
|
|
+ JSONArray simChannels = new JSONArray();
|
|
|
+
|
|
|
+ for (Conversation i: conversationBySimcard.getValue()) {
|
|
|
+ try {
|
|
|
+ simChannels.put(i.toJSON());
|
|
|
+ } catch (JSONException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot stringify conversation " +i.getAddress(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ simConversations.put("channels", simChannels);
|
|
|
+ staticContent.put("SIM|" +conversationBySimcard.getKey(), simConversations);
|
|
|
+ } catch (JSONException e) {
|
|
|
+ log.log(Level.SEVERE, "Cannot stringify conversations for sim id " +conversationBySimcard.getKey(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public abstract class TypedCallback <T>{
|
|
|
+ public abstract void onResult(T result);
|
|
|
+ }
|
|
|
+}
|