1package eu.siacs.conversations.ui;
2
3import org.openintents.openpgp.OpenPgpError;
4
5import eu.siacs.conversations.R;
6import eu.siacs.conversations.crypto.OnPgpEngineResult;
7import eu.siacs.conversations.entities.Account;
8import eu.siacs.conversations.entities.Conversation;
9import eu.siacs.conversations.entities.Message;
10import eu.siacs.conversations.services.XmppConnectionService;
11import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
12import eu.siacs.conversations.utils.ExceptionHelper;
13import android.app.Activity;
14import android.app.AlertDialog;
15import android.app.PendingIntent;
16import android.app.AlertDialog.Builder;
17import android.content.ComponentName;
18import android.content.Context;
19import android.content.DialogInterface;
20import android.content.DialogInterface.OnClickListener;
21import android.content.IntentSender.SendIntentException;
22import android.content.Intent;
23import android.content.ServiceConnection;
24import android.net.Uri;
25import android.os.Bundle;
26import android.os.IBinder;
27import android.util.Log;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.inputmethod.InputMethodManager;
31
32public abstract class XmppActivity extends Activity {
33
34 public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
35
36 protected final static String LOGTAG = "xmppService";
37
38 public XmppConnectionService xmppConnectionService;
39 public boolean xmppConnectionServiceBound = false;
40 protected boolean handledViewIntent = false;
41
42 protected ServiceConnection mConnection = new ServiceConnection() {
43
44 @Override
45 public void onServiceConnected(ComponentName className, IBinder service) {
46 XmppConnectionBinder binder = (XmppConnectionBinder) service;
47 xmppConnectionService = binder.getService();
48 xmppConnectionServiceBound = true;
49 onBackendConnected();
50 }
51
52 @Override
53 public void onServiceDisconnected(ComponentName arg0) {
54 xmppConnectionServiceBound = false;
55 }
56 };
57
58 @Override
59 protected void onStart() {
60 super.onStart();
61 if (!xmppConnectionServiceBound) {
62 connectToBackend();
63 }
64 }
65
66 public void connectToBackend() {
67 Intent intent = new Intent(this, XmppConnectionService.class);
68 intent.setAction("ui");
69 startService(intent);
70 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
71 }
72
73 @Override
74 protected void onStop() {
75 super.onStop();
76 if (xmppConnectionServiceBound) {
77 unbindService(mConnection);
78 xmppConnectionServiceBound = false;
79 }
80 }
81
82 protected void hideKeyboard() {
83 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
84
85 View focus = getCurrentFocus();
86
87 if (focus != null) {
88
89 inputManager.hideSoftInputFromWindow(
90 focus.getWindowToken(),
91 InputMethodManager.HIDE_NOT_ALWAYS);
92 }
93 }
94
95 public boolean hasPgp() {
96 if (xmppConnectionService.getPgpEngine()!=null) {
97 return true;
98 } else {
99 Builder builder = new AlertDialog.Builder(this);
100 builder.setTitle(getString(R.string.openkeychain_required));
101 builder.setIconAttribute(android.R.attr.alertDialogIcon);
102 builder.setMessage(getText(R.string.openkeychain_required_long));
103 builder.setNegativeButton(getString(R.string.cancel), null);
104 builder.setNeutralButton(getString(R.string.restart), new OnClickListener() {
105
106 @Override
107 public void onClick(DialogInterface dialog, int which) {
108 if (xmppConnectionServiceBound) {
109 unbindService(mConnection);
110 xmppConnectionServiceBound = false;
111 }
112 stopService(new Intent(XmppActivity.this, XmppConnectionService.class));
113 finish();
114 }
115 });
116 builder.setPositiveButton(getString(R.string.install), new OnClickListener() {
117
118 @Override
119 public void onClick(DialogInterface dialog, int which) {
120 Uri uri = Uri.parse("market://details?id=org.sufficientlysecure.keychain");
121 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
122 startActivity(intent);
123 finish();
124 }
125 });
126 builder.create().show();
127 return false;
128 }
129 }
130
131 abstract void onBackendConnected();
132
133 public boolean onOptionsItemSelected(MenuItem item) {
134 switch (item.getItemId()) {
135 case R.id.action_settings:
136 startActivity(new Intent(this, SettingsActivity.class));
137 break;
138 case R.id.action_accounts:
139 startActivity(new Intent(this, ManageAccountActivity.class));
140 break;
141 }
142 return super.onOptionsItemSelected(item);
143 }
144
145 @Override
146 protected void onCreate(Bundle savedInstanceState) {
147 super.onCreate(savedInstanceState);
148 ExceptionHelper.init(getApplicationContext());
149 }
150
151 public void switchToConversation(Conversation conversation, String text) {
152 Intent viewConversationIntent = new Intent(this,
153 ConversationActivity.class);
154 viewConversationIntent.setAction(Intent.ACTION_VIEW);
155 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
156 conversation.getUuid());
157 if (text!=null) {
158 viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
159 }
160 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
161 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
162 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
163 startActivity(viewConversationIntent);
164 }
165
166 protected void announcePgp(final Account account, final Conversation conversation) {
167 xmppConnectionService.getPgpEngine().generateSignature(account, "online", new OnPgpEngineResult() {
168
169 @Override
170 public void userInputRequried(PendingIntent pi) {
171 try {
172 startIntentSenderForResult(pi.getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
173 } catch (SendIntentException e) {
174 Log.d("xmppService","coulnd start intent for pgp anncouncment");
175 }
176 }
177
178 @Override
179 public void success() {
180 xmppConnectionService.databaseBackend.updateAccount(account);
181 xmppConnectionService.sendPgpPresence(account, account.getPgpSignature());
182 if (conversation!=null) {
183 conversation.setNextEncryption(Message.ENCRYPTION_PGP);
184 }
185 }
186
187 @Override
188 public void error(OpenPgpError openPgpError) {
189 // TODO Auto-generated method stub
190
191 }
192 });
193 }
194}