package me.teafry.voipmessages; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import java.util.ArrayList; import java.util.List; /** * sms:/smsto:/mms:/mmsto: links from other apps ("Message" on a contact, a * number on a web page): opens that chat, with any text they passed as the draft. */ public final class SendToActivity extends Activity { /** The conversation key for an sms: link's recipients ("" if none). */ static String contact(Uri uri) { String ssp = uri.getSchemeSpecificPart(); if (ssp == null) return ""; int q = ssp.indexOf('?'); if (q >= 0) ssp = ssp.substring(0, q); List nums = new ArrayList<>(); for (String s : ssp.split("[,;]")) { String n = Sim.contactFor(s); if (!n.isEmpty()) nums.add(n); } if (nums.isEmpty()) return ""; return nums.size() == 1 ? nums.get(0) : Phone.groupKey(nums); } @Override protected void onCreate(Bundle b) { super.onCreate(b); Intent in = getIntent(); Uri data = in.getData(); String contact = data == null ? "" : contact(data); String body = in.getStringExtra("sms_body"); if (body == null) body = in.getStringExtra(Intent.EXTRA_TEXT); if (body == null && data != null && data.isHierarchical()) body = data.getQueryParameter("body"); if (body == null && data != null) { String ssp = data.getSchemeSpecificPart(); int q = ssp == null ? -1 : ssp.indexOf("body="); if (q >= 0) body = Uri.decode(ssp.substring(q + 5)); } if (contact.isEmpty()) { startActivity(new Intent(this, NewChatActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); finish(); return; } if (body != null && !body.trim().isEmpty()) getSharedPreferences("drafts", MODE_PRIVATE).edit().putString(contact, body).apply(); startActivity(ChatActivity.intent(this, didFor(contact), contact)); finish(); } /** An existing chat with them from any number; otherwise the SIM. */ private String didFor(String contact) { for (Store.Conv c : Store.get(this).conversations(null)) if (c.contact.equals(contact)) return c.did; Config cfg = Config.get(this); for (Config.Did d : cfg.dids) if (d.sim) return d.did; return cfg.defaultDid != null ? cfg.defaultDid : Sim.DID; } }