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 Intent intent = new Intent(this, XmppConnectionService.class);
47 intent.setAction("ui");
48 startService(intent);
49 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
50 }
51 }
52
53 @Override
54 protected void onStop() {
55 super.onStop();
56 if (xmppConnectionServiceBound) {
57 unbindService(mConnection);
58 xmppConnectionServiceBound = false;
59 }
60 }
61
62 protected void hideKeyboard() {
63 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
64
65 View focus = getCurrentFocus();
66
67 if (focus != null) {
68
69 inputManager.hideSoftInputFromWindow(
70 focus.getWindowToken(),
71 InputMethodManager.HIDE_NOT_ALWAYS);
72 }
73 }
74
75 public boolean hasPgp() {
76 if (xmppConnectionService.getPgpEngine()!=null) {
77 return true;
78 } else {
79 Builder builder = new AlertDialog.Builder(this);
80 builder.setTitle("OpenKeychain not found");
81 builder.setIconAttribute(android.R.attr.alertDialogIcon);
82 builder.setMessage("Please make sure you have installed OpenKeychain");
83 builder.create().show();
84 return false;
85 }
86 }
87
88 abstract void onBackendConnected();
89
90 public boolean onOptionsItemSelected(MenuItem item) {
91 switch (item.getItemId()) {
92 case R.id.action_settings:
93 startActivity(new Intent(this, SettingsActivity.class));
94 break;
95 case R.id.action_accounts:
96 startActivity(new Intent(this, ManageAccountActivity.class));
97 break;
98 }
99 return super.onOptionsItemSelected(item);
100 }
101
102 @Override
103 protected void onCreate(Bundle savedInstanceState) {
104 super.onCreate(savedInstanceState);
105 ExceptionHelper.init(getApplicationContext());
106 }
107
108 public void switchToConversation(Conversation conversation, String text) {
109 Intent viewConversationIntent = new Intent(this,
110 ConversationActivity.class);
111 viewConversationIntent.setAction(Intent.ACTION_VIEW);
112 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
113 conversation.getUuid());
114 if (text!=null) {
115 viewConversationIntent.putExtra(ConversationActivity.TEXT, text);
116 }
117 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
118 viewConversationIntent.setFlags(viewConversationIntent.getFlags()
119 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
120 startActivity(viewConversationIntent);
121 }
122}