/** * Copyright (C) 2014 Open Whisper Systems * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.securecomcode.text; import android.content.Intent; import android.database.ContentObserver; import android.os.AsyncTask; import android.os.Bundle; import android.provider.ContactsContract; import android.util.Log; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.securecomcode.text.service.DirectoryRefreshListener; import com.securecomcode.text.database.DatabaseFactory; import com.securecomcode.text.notifications.MessageNotifier; import com.securecomcode.text.recipients.RecipientFactory; import com.securecomcode.text.recipients.Recipients; import com.securecomcode.text.service.KeyCachingService; import com.securecomcode.text.service.SendReceiveService; import com.securecomcode.text.util.DynamicLanguage; import com.securecomcode.text.util.DynamicTheme; import com.securecomcode.text.util.MemoryCleaner; import com.securecomcode.text.util.TextSecurePreferences; import org.whispersystems.textsecure.crypto.MasterSecret; public class ConversationListActivity extends PassphraseRequiredSherlockFragmentActivity implements ConversationListFragment.ConversationSelectedListener { private final DynamicTheme dynamicTheme = new DynamicTheme (); private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); private ConversationListFragment fragment; private MasterSecret masterSecret; private ContentObserver observer; @Override public void onCreate(Bundle icicle) { dynamicTheme.onCreate(this); dynamicLanguage.onCreate(this); super.onCreate(icicle); setContentView(R.layout.conversation_list_activity); getSupportActionBar().setTitle(R.string.app_name); initializeSenderReceiverService(); initializeResources(); initializeContactUpdatesReceiver(); DirectoryRefreshListener.schedule(this); } @Override public void onPostCreate(Bundle bundle) { super.onPostCreate(bundle); } @Override public void onResume() { super.onResume(); dynamicTheme.onResume(this); dynamicLanguage.onResume(this); } @Override public void onDestroy() { Log.w("ConversationListActivity", "onDestroy..."); MemoryCleaner.clean(masterSecret); if (observer != null) getContentResolver().unregisterContentObserver(observer); super.onDestroy(); } @Override public void onMasterSecretCleared() { // this.fragment.setMasterSecret(null); startActivity(new Intent(this, RoutingActivity.class)); super.onMasterSecretCleared(); } @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuInflater inflater = this.getSupportMenuInflater(); menu.clear(); inflater.inflate(R.menu.text_secure_normal, menu); menu.findItem(R.id.menu_clear_passphrase).setVisible(!TextSecurePreferences.isPasswordDisabled(this)); super.onPrepareOptionsMenu(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case R.id.menu_new_message: openSingleContactSelection(); return true; /*case R.id.menu_new_group: createGroup(); return true;*/ case R.id.menu_settings: handleDisplaySettings(); return true; case R.id.menu_clear_passphrase: handleClearPassphrase(); return true; case R.id.menu_mark_all_read: handleMarkAllRead(); return true; case R.id.menu_import_export: handleImportExport(); return true; case R.id.menu_my_identity: handleMyIdentity(); return true; } return false; } @Override public void onCreateConversation(long threadId, Recipients recipients, int distributionType) { createConversation(threadId, recipients, distributionType); } private void createGroup() { Intent intent = new Intent(this, GroupCreateActivity.class); intent.putExtra("master_secret", masterSecret); startActivity(intent); } private void openSingleContactSelection() { Intent intent = new Intent(this, NewConversationActivity.class); intent.putExtra(NewConversationActivity.MASTER_SECRET_EXTRA, masterSecret); startActivity(intent); } private void createConversation(long threadId, Recipients recipients, int distributionType) { Intent intent = new Intent(this, ConversationActivity.class); intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.toIdString()); intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId); intent.putExtra(ConversationActivity.MASTER_SECRET_EXTRA, masterSecret); intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, distributionType); startActivity(intent); } private void handleDisplaySettings() { Intent preferencesIntent = new Intent(this, ApplicationPreferencesActivity.class); preferencesIntent.putExtra("master_secret", masterSecret); startActivity(preferencesIntent); } private void handleClearPassphrase() { Intent intent = new Intent(this, KeyCachingService.class); intent.setAction(KeyCachingService.CLEAR_KEY_ACTION); startService(intent); } private void handleImportExport() { final Intent intent = new Intent(this, ImportExportActivity.class); intent.putExtra("master_secret", masterSecret); startActivity(intent); } private void handleMyIdentity() { final Intent intent = new Intent(this, ViewLocalIdentityActivity.class); intent.putExtra("master_secret", masterSecret); startActivity(intent); } private void handleMarkAllRead() { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { DatabaseFactory.getThreadDatabase(ConversationListActivity.this).setAllThreadsRead(); MessageNotifier.updateNotification(ConversationListActivity.this, masterSecret); return null; } }.execute(); } private void initializeContactUpdatesReceiver() { observer = new ContentObserver(null) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Log.w("ConversationListActivity", "detected android contact data changed, refreshing cache"); // TODO only clear updated recipients from cache RecipientFactory.clearCache(); ConversationListActivity.this.runOnUiThread(new Runnable() { @Override public void run() { ((ConversationListAdapter)fragment.getListAdapter()).notifyDataSetChanged(); } }); } }; getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, observer); } private void initializeSenderReceiverService() { Intent smsSenderIntent = new Intent(SendReceiveService.SEND_SMS_ACTION, null, this, SendReceiveService.class); Intent mmsSenderIntent = new Intent(SendReceiveService.SEND_MMS_ACTION, null, this, SendReceiveService.class); startService(smsSenderIntent); startService(mmsSenderIntent); } private void initializeResources() { this.masterSecret = getIntent().getParcelableExtra("master_secret"); this.fragment = (ConversationListFragment)this.getSupportFragmentManager() .findFragmentById(R.id.fragment_content); this.fragment.setMasterSecret(masterSecret); } }