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.Intent;
14import android.content.ServiceConnection;
15import android.os.Bundle;
16import android.os.IBinder;
17import android.view.MenuItem;
18import android.view.View;
19import android.view.inputmethod.InputMethodManager;
20
21public abstract class XmppActivity extends Activity {
22 public XmppConnectionService xmppConnectionService;
23 public boolean xmppConnectionServiceBound = false;
24 protected boolean handledViewIntent = false;
25
26 protected ServiceConnection mConnection = new ServiceConnection() {
27
28 @Override
29 public void onServiceConnected(ComponentName className, IBinder service) {
30 XmppConnectionBinder binder = (XmppConnectionBinder) service;
31 xmppConnectionService = binder.getService();
32 xmppConnectionServiceBound = true;
33 onBackendConnected();
34 }
35
36 @Override
37 public void onServiceDisconnected(ComponentName arg0) {
38 xmppConnectionServiceBound = false;
39 }
40 };
41
42 @Override
43 protected void onStart() {
44 super.onStart();
45 if (!xmppConnectionServiceBound) {
46 connectToBackend();
47 }
48 }
49
50 public void connectToBackend() {
51 Intent intent = new Intent(this, XmppConnectionService.class);
52 intent.setAction("ui");
53 startService(intent);
54 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
55 }
56
57 @Override
58 protected void onStop() {
59 super.onStop();
60 if (xmppConnectionServiceBound) {
61 unbindService(mConnection);
62 xmppConnectionServiceBound = false;
63 }
64 }
65
66 protected void hideKeyboard() {
67 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
68
69 View focus = getCurrentFocus();
70
71 if (focus != null) {
72
73 inputManager.hideSoftInputFromWindow(
74 focus.getWindowToken(),
75 InputMethodManager.HIDE_NOT_ALWAYS);
76 }
77 }
78
79 public boolean hasPgp() {
80 if (xmppConnectionService.getPgpEngine()!=null) {
81 return true;
82 } else {
83 Builder builder = new AlertDialog.Builder(this);
84 builder.setTitle("OpenKeychain not found");
85 builder.setIconAttribute(android.R.attr.alertDialogIcon);
86 builder.setMessage("Please make sure you have installed OpenKeychain");
87 builder.create().show();
88 return false;
89 }
90 }
91
92 abstract void onBackendConnected();
93
94 public boolean onOptionsItemSelected(MenuItem item) {
95 switch (item.getItemId()) {
96 case R.id.action_settings:
97 startActivity(new Intent(this, SettingsActivity.class));
98 break;
99 case R.id.action_accounts:
100 startActivity(new Intent(this, ManageAccountActivity.class));
101 break;
102 }
103 return super.onOptionsItemSelected(item);
104 }
105
106 @Override
107 protected void onCreate(Bundle savedInstanceState) {
108 super.onCreate(savedInstanceState);
109 ExceptionHelper.init(getApplicationContext());
110 }
111
112 public void switchToConversation(Conversation conversation, String text) {
113 Intent viewConversationIntent = new Intent(this,
114 ConversationActivity.class);
115 viewConversationIntent.setAction(Intent.ACTION_VIEW);
116 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
117 conversation.getUuid());
118 if (text!=null) {
119 viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
120 }
121 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
122 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
123 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
124 startActivity(viewConversationIntent);
125 }
126}