From 8838a5094812e0540ccfef3334e49e1a5c1a564b Mon Sep 17 00:00:00 2001 From: steckbrief Date: Fri, 6 Nov 2015 20:23:43 +0100 Subject: FileBackend splitted into several util classes for separate concerns: AvatarUtil, StreamUtil, ImageUtil. Unused imports removed. --- .../services/XmppConnectionService.java | 260 ++++++++++----------- 1 file changed, 118 insertions(+), 142 deletions(-) (limited to 'src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java') diff --git a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java index 62ed2781..624914dd 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java @@ -1,14 +1,12 @@ package de.thedevstack.conversationsplus.services; import android.annotation.SuppressLint; -import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; -import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.ConnectivityManager; @@ -23,9 +21,7 @@ import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.os.SystemClock; import android.provider.ContactsContract; -import android.provider.MediaStore; import android.util.Log; -import android.util.LruCache; import net.java.otr4j.OtrException; import net.java.otr4j.session.Session; @@ -36,7 +32,6 @@ import net.java.otr4j.session.SessionStatus; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -57,10 +52,10 @@ import java.util.concurrent.CopyOnWriteArrayList; import de.duenndns.ssl.MemorizingTrustManager; import de.thedevstack.conversationsplus.ConversationsPlusApplication; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; -import de.thedevstack.conversationsplus.enums.UserDecision; -import de.thedevstack.conversationsplus.ui.dialogs.UserDecisionDialog; -import de.thedevstack.conversationsplus.ui.listeners.ResizePictureUserDecisionListener; +import de.thedevstack.conversationsplus.exceptions.FileCopyException; +import de.thedevstack.conversationsplus.utils.AvatarUtil; import de.thedevstack.conversationsplus.utils.FileHelper; +import de.thedevstack.conversationsplus.utils.ImageUtil; import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.R; @@ -215,7 +210,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } }; - private FileBackend fileBackend = new FileBackend(this); private MemorizingTrustManager mMemorizingTrustManager; private NotificationService mNotificationService = new NotificationService( this); @@ -322,7 +316,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private PgpEngine mPgpEngine = null; private WakeLock wakeLock; private PowerManager pm; - private LruCache mBitmapCache; private Thread mPhoneContactMergerThread; private boolean mRestoredFromDatabase = false; @@ -344,10 +337,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } - public FileBackend getFileBackend() { - return this.fileBackend; - } - public AvatarService getAvatarService() { return this.mAvatarService; } @@ -384,10 +373,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } message.setCounterpart(conversation.getNextCounterpart()); message.setType(Message.TYPE_FILE); - String path = getFileBackend().getOriginalPath(uri); + String path = FileHelper.getRealPathFromUri(uri); if (path!=null) { message.setRelativeFilePath(path); - getFileBackend().updateFileParams(message); + FileBackend.updateFileParams(message); if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { getPgpEngine().encrypt(message, callback); } else { @@ -395,21 +384,21 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } else { mFileAddingExecutor.execute(new Runnable() { - @Override - public void run() { - try { - getFileBackend().copyFileToPrivateStorage(message, uri); - getFileBackend().updateFileParams(message); - if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { - getPgpEngine().encrypt(message, callback); - } else { - callback.success(message); - } - } catch (FileBackend.FileCopyException e) { - callback.error(e.getResId(), message); - } - } - }); + @Override + public void run() { + try { + FileBackend.copyFileToPrivateStorage(message, uri); + FileBackend.updateFileParams(message); + if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { + getPgpEngine().encrypt(message, callback); + } else { + callback.success(message); + } + } catch (FileCopyException e) { + callback.error(e.getResId(), message); + } + } + }); } } @@ -477,13 +466,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa @Override public void run() { try { - getFileBackend().copyImageToPrivateStorage(message, uri); + FileBackend.copyImageToPrivateStorage(message, uri); if (conversation.getNextEncryption(forceEncryption) == Message.ENCRYPTION_PGP) { getPgpEngine().encrypt(message, callback); } else { callback.success(message); } - } catch (final FileBackend.FileCopyException e) { + } catch (final FileCopyException e) { callback.error(e.getResId(), message); } } @@ -659,14 +648,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa PRNGFixes.apply(); this.mRandom = new SecureRandom(); updateMemorizingTrustmanager(); - final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); - final int cacheSize = maxMemory / 8; - this.mBitmapCache = new LruCache(cacheSize) { - @Override - protected int sizeOf(final String key, final Bitmap bitmap) { - return bitmap.getByteCount() / 1024; - } - }; this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); this.accounts = databaseBackend.getAccounts(); @@ -908,7 +889,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } else { Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching roster"); } - iqPacket.query(Xmlns.ROSTER).setAttribute("ver",account.getRosterVersion()); + iqPacket.query(Xmlns.ROSTER).setAttribute("ver", account.getRosterVersion()); sendIqPacket(account, iqPacket, mIqParser); } @@ -1022,7 +1003,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa for(Account account : accounts) { databaseBackend.readRoster(account.getRoster()); } - getBitmapCache().evictAll(); + ImageUtil.evictBitmapCache(); Looper.prepare(); PhoneHelper.loadPhoneContacts(getApplicationContext(), new CopyOnWriteArrayList(), @@ -1050,7 +1031,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa @Override public void onMessageFound(Message message) { - if (!getFileBackend().isFileAvailable(message)) { + if (!FileBackend.isFileAvailable(message)) { message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED)); } } @@ -1061,7 +1042,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa for (Conversation conversation : getConversations()) { Message message = conversation.findMessageWithFileAndUuid(uuid); if (message != null) { - if (!getFileBackend().isFileAvailable(message)) { + if (!FileBackend.isFileAvailable(message)) { message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_DELETED)); updateConversationUi(); } @@ -1087,19 +1068,19 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } Collections.sort(list, new Comparator() { - @Override - public int compare(Conversation lhs, Conversation rhs) { - Message left = lhs.getLatestMessage(); - Message right = rhs.getLatestMessage(); - if (left.getTimeSent() > right.getTimeSent()) { - return -1; - } else if (left.getTimeSent() < right.getTimeSent()) { - return 1; - } else { - return 0; - } - } - }); + @Override + public int compare(Conversation lhs, Conversation rhs) { + Message left = lhs.getLatestMessage(); + Message right = rhs.getLatestMessage(); + if (left.getTimeSent() > right.getTimeSent()) { + return -1; + } else if (left.getTimeSent() < right.getTimeSent()) { + return 1; + } else { + return 0; + } + } + }); } public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) { @@ -1271,17 +1252,17 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) { final IqPacket iq = getIqGenerator().generateSetPassword(account, newPassword); sendIqPacket(account, iq, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(final Account account, final IqPacket packet) { - if (packet.getType() == IqPacket.TYPE.RESULT) { - account.setPassword(newPassword); - databaseBackend.updateAccount(account); - callback.onPasswordChangeSucceeded(); - } else { - callback.onPasswordChangeFailed(); - } - } - }); + @Override + public void onIqPacketReceived(final Account account, final IqPacket packet) { + if (packet.getType() == IqPacket.TYPE.RESULT) { + account.setPassword(newPassword); + databaseBackend.updateAccount(account); + callback.onPasswordChangeSucceeded(); + } else { + callback.onPasswordChangeFailed(); + } + } + }); } public void deleteAccount(final Account account) { @@ -1616,7 +1597,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa conversation.getMucOptions().setOffline(); conversation.deregisterWithBookmark(); Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() - + ": leaving muc " + conversation.getJid()); + + ": leaving muc " + conversation.getJid()); } else { account.pendingConferenceLeaves.add(conversation); } @@ -1700,23 +1681,23 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa request.setTo(conversation.getJid().toBareJid()); request.query("http://jabber.org/protocol/disco#info"); sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - if (packet.getType() != IqPacket.TYPE.ERROR) { - ArrayList features = new ArrayList<>(); - for (Element child : packet.query().getChildren()) { - if (child != null && child.getName().equals("feature")) { - String var = child.getAttribute("var"); - if (var != null) { - features.add(var); - } - } - } - conversation.getMucOptions().updateFeatures(features); - updateConversationUi(); - } - } - }); + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + if (packet.getType() != IqPacket.TYPE.ERROR) { + ArrayList features = new ArrayList<>(); + for (Element child : packet.query().getChildren()) { + if (child != null && child.getName().equals("feature")) { + String var = child.getAttribute("var"); + if (var != null) { + features.add(var); + } + } + } + conversation.getMucOptions().updateFeatures(features); + updateConversationUi(); + } + } + }); } public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConferenceOptionsPushed callback) { @@ -1876,27 +1857,27 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa + otrSession.getSessionID().getUserID()); conversation.findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() { - @Override - public void onMessageFound(Message message) { - SessionID id = otrSession.getSessionID(); - try { - message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID())); - } catch (InvalidJidException e) { - return; - } - if (message.needsUploading()) { - mJingleConnectionManager.createNewConnection(message); - } else { - MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true); - if (outPacket != null) { - message.setStatus(Message.STATUS_SEND); - databaseBackend.updateMessage(message); - sendMessagePacket(account, outPacket); - } - } - updateConversationUi(); - } - }); + @Override + public void onMessageFound(Message message) { + SessionID id = otrSession.getSessionID(); + try { + message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID())); + } catch (InvalidJidException e) { + return; + } + if (message.needsUploading()) { + mJingleConnectionManager.createNewConnection(message); + } else { + MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true); + if (outPacket != null) { + message.setStatus(Message.STATUS_SEND); + databaseBackend.updateMessage(message); + sendMessagePacket(account, outPacket); + } + } + updateConversationUi(); + } + }); } public boolean renewSymmetricKey(Conversation conversation) { @@ -1954,8 +1935,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa final UiCallback callback) { final Bitmap.CompressFormat format = Config.AVATAR_FORMAT; final int size = Config.AVATAR_SIZE; - final Avatar avatar = getFileBackend() - .getPepAvatar(image, size, format); + final Avatar avatar = AvatarUtil.getPepAvatar(image, size, format); if (avatar != null) { avatar.height = size; avatar.width = size; @@ -1966,7 +1946,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } else if (format.equals(Bitmap.CompressFormat.PNG)) { avatar.type = "image/png"; } - if (!getFileBackend().save(avatar)) { + if (!AvatarUtil.save(avatar)) { callback.error(R.string.error_saving_avatar, avatar); return; } @@ -2050,7 +2030,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (result.getType() == IqPacket.TYPE.RESULT) { avatar.image = mIqParser.avatarData(result); if (avatar.image != null) { - if (getFileBackend().save(avatar)) { + if (AvatarUtil.save(avatar)) { if (account.getJid().toBareJid().equals(avatar.owner)) { if (account.setAvatar(avatar.getFilename())) { databaseBackend.updateAccount(account); @@ -2096,31 +2076,31 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private void fetchAvatarVcard(final Account account, final Avatar avatar, final UiCallback callback) { IqPacket packet = this.mIqGenerator.retrieveVcardAvatar(avatar); this.sendIqPacket(account, packet, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - synchronized (mInProgressAvatarFetches) { - mInProgressAvatarFetches.remove(generateFetchKey(account, avatar)); - } - if (packet.getType() == IqPacket.TYPE.RESULT) { - Element vCard = packet.findChild("vCard", "vcard-temp"); - Element photo = vCard != null ? vCard.findChild("PHOTO") : null; - String image = photo != null ? photo.findChildContent("BINVAL") : null; - if (image != null) { - avatar.image = image; - if (getFileBackend().save(avatar)) { - Log.d(Config.LOGTAG, account.getJid().toBareJid() - + ": successfully fetched vCard avatar for " + avatar.owner); - Contact contact = account.getRoster() - .getContact(avatar.owner); - contact.setAvatar(avatar); - getAvatarService().clear(contact); - updateConversationUi(); - updateRosterUi(); - } - } - } - } - }); + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + synchronized (mInProgressAvatarFetches) { + mInProgressAvatarFetches.remove(generateFetchKey(account, avatar)); + } + if (packet.getType() == IqPacket.TYPE.RESULT) { + Element vCard = packet.findChild("vCard", "vcard-temp"); + Element photo = vCard != null ? vCard.findChild("PHOTO") : null; + String image = photo != null ? photo.findChildContent("BINVAL") : null; + if (image != null) { + avatar.image = image; + if (AvatarUtil.save(avatar)) { + Log.d(Config.LOGTAG, account.getJid().toBareJid() + + ": successfully fetched vCard avatar for " + avatar.owner); + Contact contact = account.getRoster() + .getContact(avatar.owner); + contact.setAvatar(avatar); + getAvatarService().clear(contact); + updateConversationUi(); + updateRosterUi(); + } + } + } + } + }); } public void checkForAvatar(Account account, final UiCallback callback) { @@ -2138,7 +2118,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa Avatar avatar = Avatar.parseMetadata(items); if (avatar != null) { avatar.owner = account.getJid().toBareJid(); - if (fileBackend.isAvatarCached(avatar)) { + if (AvatarUtil.isAvatarCached(avatar)) { if (account.setAvatar(avatar.getFilename())) { databaseBackend.updateAccount(account); } @@ -2401,10 +2381,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa return this.pm; } - public LruCache getBitmapCache() { - return this.mBitmapCache; - } - public void syncRosterToDisk(final Account account) { Runnable runnable = new Runnable() { -- cgit v1.2.3