1 | /* This file is part of Aard Dictionary for Android <http://aarddict.org>. |
2 | * |
3 | * This program is free software: you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License version 3 |
5 | * as published by the Free Software Foundation. |
6 | * |
7 | * This program is distributed in the hope that it will be useful, |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | * GNU General Public License <http://www.gnu.org/licenses/gpl-3.0.txt> |
11 | * for more details. |
12 | * |
13 | * Copyright (C) 2010 Igor Tkach |
14 | */ |
15 | |
16 | package aarddict.android; |
17 | |
18 | import android.app.Activity; |
19 | import android.app.ProgressDialog; |
20 | import android.content.BroadcastReceiver; |
21 | import android.content.ComponentName; |
22 | import android.content.Context; |
23 | import android.content.Intent; |
24 | import android.content.IntentFilter; |
25 | import android.content.ServiceConnection; |
26 | import android.os.IBinder; |
27 | import android.util.Log; |
28 | import android.view.Window; |
29 | import android.widget.Toast; |
30 | |
31 | abstract class BaseDictionaryActivity extends Activity { |
32 | |
33 | private final static String TAG = BaseDictionaryActivity.class |
34 | .getName(); |
35 | |
36 | protected final static String ACTION_NO_DICTIONARIES = "aarddict.android.ACTION_NO_DICTIONARIES"; |
37 | |
38 | protected BroadcastReceiver broadcastReceiver; |
39 | protected DictionaryService dictionaryService; |
40 | |
41 | |
42 | private ServiceConnection connection = new ServiceConnection() { |
43 | public void onServiceConnected(ComponentName className, IBinder service) { |
44 | dictionaryService = ((DictionaryService.LocalBinder)service).getService(); |
45 | Log.d(TAG, "Service connected: " + dictionaryService); |
46 | onDictionaryServiceConnected(); |
47 | } |
48 | |
49 | public void onServiceDisconnected(ComponentName className) { |
50 | Log.d(TAG, "Service disconnected: " + dictionaryService); |
51 | dictionaryService = null; |
52 | Toast.makeText(BaseDictionaryActivity.this, "Dictionary service disconnected, quitting...", |
53 | Toast.LENGTH_LONG).show(); |
54 | finish(); |
55 | } |
56 | }; |
57 | |
58 | private void registerProgressReceiver() { |
59 | IntentFilter intentFilter = new IntentFilter(); |
60 | intentFilter.addAction(DictionaryService.DICT_OPEN_FAILED); |
61 | intentFilter.addAction(DictionaryService.DISCOVERY_STARTED); |
62 | intentFilter.addAction(DictionaryService.DISCOVERY_FINISHED); |
63 | intentFilter.addAction(DictionaryService.OPEN_FINISHED); |
64 | intentFilter.addAction(DictionaryService.OPEN_STARTED); |
65 | intentFilter.addAction(DictionaryService.OPENED_DICT); |
66 | |
67 | broadcastReceiver = new BroadcastReceiver() { |
68 | |
69 | ProgressDialog discoveryProgress; |
70 | ProgressDialog openProgress; |
71 | |
72 | @Override |
73 | public void onReceive(Context context, Intent intent) { |
74 | String a = intent.getAction(); |
75 | if (a.equals(DictionaryService.DISCOVERY_STARTED)) { |
76 | Log.d(TAG, "dictionary disconvery started"); |
77 | if (discoveryProgress == null) { |
78 | discoveryProgress = new DiscoveryProgressDialog(context); |
79 | } |
80 | discoveryProgress.show(); |
81 | } else |
82 | if (a.equals(DictionaryService.DISCOVERY_FINISHED)) { |
83 | Log.d(TAG, "dictionary discovery finished"); |
84 | if (discoveryProgress != null) { |
85 | discoveryProgress.dismiss(); |
86 | discoveryProgress = null; |
87 | } |
88 | } else |
89 | if (a.equals(DictionaryService.OPEN_STARTED)) { |
90 | Log.d(TAG, "dictionary open started"); |
91 | int count = intent.getIntExtra("count", 0); |
92 | if (openProgress == null) { |
93 | openProgress = new OpeningProgressDialog(context); |
94 | } |
95 | openProgress.setMax(count); |
96 | openProgress.show(); |
97 | } else |
98 | if (a.equals(DictionaryService.DICT_OPEN_FAILED) || |
99 | a.equals(DictionaryService.OPENED_DICT)) { |
100 | if (openProgress != null) { |
101 | openProgress.incrementProgressBy(1); |
102 | } |
103 | } else |
104 | if (a.equals(DictionaryService.OPEN_FINISHED)) { |
105 | if (openProgress != null) { |
106 | openProgress.dismiss(); |
107 | openProgress = null; |
108 | } |
109 | onDictionaryOpenFinished(); |
110 | } |
111 | } |
112 | }; |
113 | registerReceiver(broadcastReceiver, intentFilter); |
114 | } |
115 | |
116 | protected void onCreate(android.os.Bundle savedInstanceState) { |
117 | super.onCreate(savedInstanceState); |
118 | getWindow().requestFeature(Window.FEATURE_LEFT_ICON); |
119 | registerProgressReceiver(); |
120 | initUI(); |
121 | getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.aarddict); |
122 | Intent dictServiceIntent = new Intent(this, DictionaryService.class); |
123 | startService(dictServiceIntent); |
124 | bindService(dictServiceIntent, connection, 0); |
125 | }; |
126 | |
127 | abstract void initUI(); |
128 | |
129 | void onDictionaryServiceConnected() { |
130 | if (dictionaryService.getDictionaries().isEmpty()) { |
131 | new Thread(new Runnable() { |
132 | public void run() { |
133 | dictionaryService.openDictionaries(); |
134 | Log.d(TAG, |
135 | String.format("After openDictionaries() we have %d dictionaries", |
136 | dictionaryService.getDictionaries().size())); |
137 | if (dictionaryService.getDictionaries().isEmpty()) { |
138 | runOnUiThread(new Runnable() { |
139 | public void run() { |
140 | Intent next = new Intent(); |
141 | next.setAction(ACTION_NO_DICTIONARIES); |
142 | next.setClass(getApplicationContext(), DictionariesActivity.class); |
143 | Log.d(TAG, "No dictionaries, starting Dictionaries activity"); |
144 | startActivity(next); |
145 | finish(); |
146 | } |
147 | }); |
148 | } else { |
149 | runOnUiThread(new Runnable() { |
150 | public void run() { |
151 | onDictionaryServiceReady(); |
152 | } |
153 | }); |
154 | } |
155 | } |
156 | }).start(); |
157 | } |
158 | else { |
159 | onDictionaryServiceReady(); |
160 | } |
161 | }; |
162 | |
163 | abstract void onDictionaryServiceReady(); |
164 | void onDictionaryOpenFinished(){}; |
165 | |
166 | protected void onDestroy() { |
167 | super.onDestroy(); |
168 | unregisterReceiver(broadcastReceiver); |
169 | unbindService(connection); |
170 | } |
171 | } |