1package eu.siacs.conversations.ui;
2
3import eu.siacs.conversations.R;
4import eu.siacs.conversations.entities.Conversation;
5import eu.siacs.conversations.services.XmppConnectionService;
6import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
7import eu.siacs.conversations.utils.ExceptionHelper;
8import android.app.Activity;
9import android.app.AlertDialog;
10import android.app.AlertDialog.Builder;
11import android.content.ComponentName;
12import android.content.Context;
13import android.content.DialogInterface;
14import android.content.DialogInterface.OnClickListener;
15import android.content.Intent;
16import android.content.ServiceConnection;
17import android.net.Uri;
18import android.os.Bundle;
19import android.os.IBinder;
20import android.view.MenuItem;
21import android.view.View;
22import android.view.inputmethod.InputMethodManager;
23
24public abstract class XmppActivity extends Activity {
25
26 protected final static String LOGTAG = "xmppService";
27
28 public XmppConnectionService xmppConnectionService;
29 public boolean xmppConnectionServiceBound = false;
30 protected boolean handledViewIntent = false;
31
32 protected ServiceConnection mConnection = new ServiceConnection() {
33
34 @Override
35 public void onServiceConnected(ComponentName className, IBinder service) {
36 XmppConnectionBinder binder = (XmppConnectionBinder) service;
37 xmppConnectionService = binder.getService();
38 xmppConnectionServiceBound = true;
39 onBackendConnected();
40 }
41
42 @Override
43 public void onServiceDisconnected(ComponentName arg0) {
44 xmppConnectionServiceBound = false;
45 }
46 };
47
48 @Override
49 protected void onStart() {
50 super.onStart();
51 if (!xmppConnectionServiceBound) {
52 connectToBackend();
53 }
54 }
55
56 public void connectToBackend() {
57 Intent intent = new Intent(this, XmppConnectionService.class);
58 intent.setAction("ui");
59 startService(intent);
60 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
61 }
62
63 @Override
64 protected void onStop() {
65 super.onStop();
66 if (xmppConnectionServiceBound) {
67 unbindService(mConnection);
68 xmppConnectionServiceBound = false;
69 }
70 }
71
72 protected void hideKeyboard() {
73 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
74
75 View focus = getCurrentFocus();
76
77 if (focus != null) {
78
79 inputManager.hideSoftInputFromWindow(
80 focus.getWindowToken(),
81 InputMethodManager.HIDE_NOT_ALWAYS);
82 }
83 }
84
85 public boolean hasPgp() {
86 if (xmppConnectionService.getPgpEngine()!=null) {
87 return true;
88 } else {
89 Builder builder = new AlertDialog.Builder(this);
90 builder.setTitle(getString(R.string.openkeychain_required));
91 builder.setIconAttribute(android.R.attr.alertDialogIcon);
92 builder.setMessage(getText(R.string.openkeychain_required_long));
93 builder.setNegativeButton(getString(R.string.cancel), null);
94 builder.setNeutralButton(getString(R.string.restart), new OnClickListener() {
95
96 @Override
97 public void onClick(DialogInterface dialog, int which) {
98 // TODO Auto-generated method stub
99
100 }
101 });
102 builder.setPositiveButton(getString(R.string.install), new OnClickListener() {
103
104 @Override
105 public void onClick(DialogInterface dialog, int which) {
106 Uri uri = Uri.parse("market://details?id=org.sufficientlysecure.keychain");
107 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
108 startActivity(intent);
109 }
110 });
111 builder.create().show();
112 return false;
113 }
114 }
115
116 abstract void onBackendConnected();
117
118 public boolean onOptionsItemSelected(MenuItem item) {
119 switch (item.getItemId()) {
120 case R.id.action_settings:
121 startActivity(new Intent(this, SettingsActivity.class));
122 break;
123 case R.id.action_accounts:
124 startActivity(new Intent(this, ManageAccountActivity.class));
125 break;
126 }
127 return super.onOptionsItemSelected(item);
128 }
129
130 @Override
131 protected void onCreate(Bundle savedInstanceState) {
132 super.onCreate(savedInstanceState);
133 ExceptionHelper.init(getApplicationContext());
134 }
135
136 public void switchToConversation(Conversation conversation, String text) {
137 Intent viewConversationIntent = new Intent(this,
138 ConversationActivity.class);
139 viewConversationIntent.setAction(Intent.ACTION_VIEW);
140 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
141 conversation.getUuid());
142 if (text!=null) {
143 viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
144 }
145 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
146 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
147 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
148 startActivity(viewConversationIntent);
149 }
150}