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