mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-22 09:58:08 +00:00
Use try-with-resources in places where it looked safe
This commit is contained in:
parent
b54b69032f
commit
a9ab77471e
@ -57,49 +57,31 @@ public class ContactsHelper {
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||
public static Map<String, String> phoneNumberLookup(Context context, String number) {
|
||||
|
||||
//Log.e("PhoneNumberLookup", number);
|
||||
|
||||
Map<String, String> contactInfo = new HashMap<>();
|
||||
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
||||
Cursor cursor;
|
||||
try {
|
||||
cursor = context.getContentResolver().query(
|
||||
uri,
|
||||
new String[]{
|
||||
PhoneLookup.DISPLAY_NAME,
|
||||
ContactsContract.PhoneLookup.PHOTO_URI
|
||||
/*, PhoneLookup.TYPE
|
||||
, PhoneLookup.LABEL
|
||||
, PhoneLookup.ID */
|
||||
},
|
||||
null, null, null);
|
||||
} catch (Exception e) {
|
||||
return contactInfo;
|
||||
String[] columns = new String[]{
|
||||
PhoneLookup.DISPLAY_NAME,
|
||||
PhoneLookup.PHOTO_URI
|
||||
/*, PhoneLookup.TYPE
|
||||
, PhoneLookup.LABEL
|
||||
, PhoneLookup.ID */
|
||||
};
|
||||
try (Cursor cursor = context.getContentResolver().query(uri, columns,null, null, null)) {
|
||||
// Take the first match only
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
contactInfo.put("name", cursor.getString(nameIndex));
|
||||
}
|
||||
|
||||
nameIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
|
||||
if (nameIndex != -1) {
|
||||
contactInfo.put("photoID", cursor.getString(nameIndex));
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
// Take the first match only
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int nameIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
contactInfo.put("name", cursor.getString(nameIndex));
|
||||
}
|
||||
|
||||
nameIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI);
|
||||
if (nameIndex != -1) {
|
||||
contactInfo.put("photoID", cursor.getString(nameIndex));
|
||||
}
|
||||
|
||||
try {
|
||||
cursor.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
if (!contactInfo.isEmpty()) {
|
||||
return contactInfo;
|
||||
}
|
||||
}
|
||||
|
||||
return contactInfo;
|
||||
}
|
||||
|
||||
@ -157,34 +139,28 @@ public class ContactsHelper {
|
||||
ArrayList<uID> toReturn = new ArrayList<>();
|
||||
|
||||
// Define the columns we want to read from the Contacts database
|
||||
final String[] projection = new String[]{
|
||||
final String[] columns = new String[]{
|
||||
ContactsContract.Contacts.LOOKUP_KEY
|
||||
};
|
||||
|
||||
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
|
||||
Cursor contactsCursor = context.getContentResolver().query(
|
||||
contactsUri,
|
||||
projection,
|
||||
null, null, null);
|
||||
if (contactsCursor != null && contactsCursor.moveToFirst()) {
|
||||
do {
|
||||
uID contactID;
|
||||
try (Cursor contactsCursor = context.getContentResolver().query(contactsUri, columns, null, null, null)) {
|
||||
if (contactsCursor != null && contactsCursor.moveToFirst()) {
|
||||
do {
|
||||
uID contactID;
|
||||
|
||||
int idIndex = contactsCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
||||
if (idIndex != -1) {
|
||||
contactID = new uID(contactsCursor.getString(idIndex));
|
||||
} else {
|
||||
// Something went wrong with this contact
|
||||
// If you are experiencing this, please open a bug report indicating how you got here
|
||||
Log.e("ContactsHelper", "Got a contact which does not have a LOOKUP_KEY");
|
||||
continue;
|
||||
}
|
||||
int idIndex = contactsCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
||||
if (idIndex != -1) {
|
||||
contactID = new uID(contactsCursor.getString(idIndex));
|
||||
} else {
|
||||
// Something went wrong with this contact
|
||||
// If you are experiencing this, please open a bug report indicating how you got here
|
||||
Log.e("ContactsHelper", "Got a contact which does not have a LOOKUP_KEY");
|
||||
continue;
|
||||
}
|
||||
|
||||
toReturn.add(contactID);
|
||||
} while (contactsCursor.moveToNext());
|
||||
try {
|
||||
contactsCursor.close();
|
||||
} catch (Exception ignored) {
|
||||
toReturn.add(contactID);
|
||||
} while (contactsCursor.moveToNext());
|
||||
}
|
||||
}
|
||||
|
||||
@ -221,14 +197,11 @@ public class ContactsHelper {
|
||||
ContactsContract.Contacts.CONTENT_MULTI_VCARD_URI,
|
||||
Uri.encode(keys.toString()));
|
||||
|
||||
InputStream input;
|
||||
;
|
||||
StringBuilder vcardJumble = new StringBuilder();
|
||||
try {
|
||||
input = context.getContentResolver().openInputStream(vcardURI);
|
||||
|
||||
try (InputStream input = context.getContentResolver().openInputStream(vcardURI)) {
|
||||
BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input));
|
||||
String line;
|
||||
|
||||
while ((line = bufferedInput.readLine()) != null) {
|
||||
vcardJumble.append(line).append('\n');
|
||||
}
|
||||
@ -263,9 +236,8 @@ public class ContactsHelper {
|
||||
for (uID ID : IDs) {
|
||||
String lookupKey = ID.toString();
|
||||
Uri vcardURI = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
|
||||
InputStream input;
|
||||
try {
|
||||
input = context.getContentResolver().openInputStream(vcardURI);
|
||||
|
||||
try (InputStream input = context.getContentResolver().openInputStream(vcardURI)) {
|
||||
|
||||
if (input == null)
|
||||
{
|
||||
@ -281,13 +253,10 @@ public class ContactsHelper {
|
||||
}
|
||||
|
||||
toReturn.put(ID, new VCardBuilder(vcard.toString()));
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
// If you are experiencing this, please open a bug report indicating how you got here
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
} catch (NullPointerException e)
|
||||
{
|
||||
} catch (NullPointerException e) {
|
||||
// If you are experiencing this, please open a bug report indicating how you got here
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -345,63 +314,59 @@ public class ContactsHelper {
|
||||
contactsArgs.add(ID.toString());
|
||||
}
|
||||
|
||||
Cursor contactsCursor = context.getContentResolver().query(
|
||||
try (Cursor contactsCursor = context.getContentResolver().query(
|
||||
contactsUri,
|
||||
lookupProjection.toArray(new String[0]),
|
||||
contactsSelection.toString(),
|
||||
contactsArgs.toArray(new String[0]), null
|
||||
);
|
||||
contactsArgs.toArray(new String[0]),
|
||||
null
|
||||
)) {
|
||||
if (contactsCursor != null && contactsCursor.moveToFirst()) {
|
||||
do {
|
||||
Map<String, Object> requestedData = new HashMap<>();
|
||||
|
||||
if (contactsCursor != null && contactsCursor.moveToFirst()) {
|
||||
do {
|
||||
Map<String, Object> requestedData = new HashMap<>();
|
||||
int lookupKeyIdx = contactsCursor.getColumnIndexOrThrow(uID.COLUMN);
|
||||
String lookupKey = contactsCursor.getString(lookupKeyIdx);
|
||||
|
||||
int lookupKeyIdx = contactsCursor.getColumnIndexOrThrow(uID.COLUMN);
|
||||
String lookupKey = contactsCursor.getString(lookupKeyIdx);
|
||||
|
||||
// For each column, collect the data from that column
|
||||
for (String column : contactsProjection) {
|
||||
int index = contactsCursor.getColumnIndex(column);
|
||||
// Since we might be getting various kinds of data, Object is the best we can do
|
||||
Object data;
|
||||
int type;
|
||||
if (index == -1) {
|
||||
// This contact didn't have the requested column? Something is very wrong.
|
||||
// If you are experiencing this, please open a bug report indicating how you got here
|
||||
Log.e("ContactsHelper", "Got a contact which does not have a requested column");
|
||||
continue;
|
||||
}
|
||||
|
||||
type = contactsCursor.getType(index);
|
||||
switch (type) {
|
||||
case Cursor.FIELD_TYPE_INTEGER:
|
||||
data = contactsCursor.getInt(index);
|
||||
break;
|
||||
case Cursor.FIELD_TYPE_FLOAT:
|
||||
data = contactsCursor.getFloat(index);
|
||||
break;
|
||||
case Cursor.FIELD_TYPE_STRING:
|
||||
data = contactsCursor.getString(index);
|
||||
break;
|
||||
case Cursor.FIELD_TYPE_BLOB:
|
||||
data = contactsCursor.getBlob(index);
|
||||
break;
|
||||
default:
|
||||
Log.e("ContactsHelper", "Got an undefined type of column " + column);
|
||||
// For each column, collect the data from that column
|
||||
for (String column : contactsProjection) {
|
||||
int index = contactsCursor.getColumnIndex(column);
|
||||
// Since we might be getting various kinds of data, Object is the best we can do
|
||||
Object data;
|
||||
int type;
|
||||
if (index == -1) {
|
||||
// This contact didn't have the requested column? Something is very wrong.
|
||||
// If you are experiencing this, please open a bug report indicating how you got here
|
||||
Log.e("ContactsHelper", "Got a contact which does not have a requested column");
|
||||
continue;
|
||||
}
|
||||
|
||||
type = contactsCursor.getType(index);
|
||||
switch (type) {
|
||||
case Cursor.FIELD_TYPE_INTEGER:
|
||||
data = contactsCursor.getInt(index);
|
||||
break;
|
||||
case Cursor.FIELD_TYPE_FLOAT:
|
||||
data = contactsCursor.getFloat(index);
|
||||
break;
|
||||
case Cursor.FIELD_TYPE_STRING:
|
||||
data = contactsCursor.getString(index);
|
||||
break;
|
||||
case Cursor.FIELD_TYPE_BLOB:
|
||||
data = contactsCursor.getBlob(index);
|
||||
break;
|
||||
default:
|
||||
Log.e("ContactsHelper", "Got an undefined type of column " + column);
|
||||
continue;
|
||||
}
|
||||
|
||||
requestedData.put(column, data);
|
||||
}
|
||||
|
||||
requestedData.put(column, data);
|
||||
}
|
||||
|
||||
toReturn.put(new uID(lookupKey), requestedData);
|
||||
} while (contactsCursor.moveToNext());
|
||||
try {
|
||||
contactsCursor.close();
|
||||
} catch (Exception ignored) {
|
||||
toReturn.put(new uID(lookupKey), requestedData);
|
||||
} while (contactsCursor.moveToNext());
|
||||
}
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
|
@ -124,29 +124,26 @@ public class SMSHelper {
|
||||
|
||||
Uri smsUri = getSMSUri();
|
||||
|
||||
Cursor smsCursor = context.getContentResolver().query(
|
||||
try (Cursor smsCursor = context.getContentResolver().query(
|
||||
smsUri,
|
||||
Message.smsColumns,
|
||||
selection,
|
||||
selectionArgs,
|
||||
null);
|
||||
|
||||
if (smsCursor != null && smsCursor.moveToFirst()) {
|
||||
do {
|
||||
HashMap<String, String> messageInfo = new HashMap<>();
|
||||
for (int columnIdx = 0; columnIdx < smsCursor.getColumnCount(); columnIdx++) {
|
||||
String colName = smsCursor.getColumnName(columnIdx);
|
||||
String body = smsCursor.getString(columnIdx);
|
||||
messageInfo.put(colName, body);
|
||||
}
|
||||
toReturn.add(new Message(messageInfo));
|
||||
} while (smsCursor.moveToNext());
|
||||
} else {
|
||||
// No SMSes available?
|
||||
}
|
||||
|
||||
if (smsCursor != null) {
|
||||
smsCursor.close();
|
||||
null)
|
||||
) {
|
||||
if (smsCursor != null && smsCursor.moveToFirst()) {
|
||||
do {
|
||||
HashMap<String, String> messageInfo = new HashMap<>();
|
||||
for (int columnIdx = 0; columnIdx < smsCursor.getColumnCount(); columnIdx++) {
|
||||
String colName = smsCursor.getColumnName(columnIdx);
|
||||
String body = smsCursor.getString(columnIdx);
|
||||
messageInfo.put(colName, body);
|
||||
}
|
||||
toReturn.add(new Message(messageInfo));
|
||||
} while (smsCursor.moveToNext());
|
||||
} else {
|
||||
// No SMSes available?
|
||||
}
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
@ -164,32 +161,29 @@ public class SMSHelper {
|
||||
|
||||
Uri conversationUri = getConversationUri();
|
||||
|
||||
Cursor conversationsCursor = context.getContentResolver().query(
|
||||
try (Cursor conversationsCursor = context.getContentResolver().query(
|
||||
conversationUri,
|
||||
Message.smsColumns,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
null)
|
||||
) {
|
||||
if (conversationsCursor != null && conversationsCursor.moveToFirst()) {
|
||||
int threadColumn = conversationsCursor.getColumnIndexOrThrow(ThreadID.lookupColumn);
|
||||
do {
|
||||
int thread = conversationsCursor.getInt(threadColumn);
|
||||
|
||||
if (conversationsCursor != null && conversationsCursor.moveToFirst()) {
|
||||
int threadColumn = conversationsCursor.getColumnIndexOrThrow(ThreadID.lookupColumn);
|
||||
do {
|
||||
int thread = conversationsCursor.getInt(threadColumn);
|
||||
|
||||
HashMap<String, String> messageInfo = new HashMap<>();
|
||||
for (int columnIdx = 0; columnIdx < conversationsCursor.getColumnCount(); columnIdx++) {
|
||||
String colName = conversationsCursor.getColumnName(columnIdx);
|
||||
String body = conversationsCursor.getString(columnIdx);
|
||||
messageInfo.put(colName, body);
|
||||
}
|
||||
toReturn.put(new ThreadID(thread), new Message(messageInfo));
|
||||
} while (conversationsCursor.moveToNext());
|
||||
} else {
|
||||
// No conversations available?
|
||||
}
|
||||
|
||||
if (conversationsCursor != null) {
|
||||
conversationsCursor.close();
|
||||
HashMap<String, String> messageInfo = new HashMap<>();
|
||||
for (int columnIdx = 0; columnIdx < conversationsCursor.getColumnCount(); columnIdx++) {
|
||||
String colName = conversationsCursor.getColumnName(columnIdx);
|
||||
String body = conversationsCursor.getString(columnIdx);
|
||||
messageInfo.put(colName, body);
|
||||
}
|
||||
toReturn.put(new ThreadID(thread), new Message(messageInfo));
|
||||
} while (conversationsCursor.moveToNext());
|
||||
} else {
|
||||
// No conversations available?
|
||||
}
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
|
@ -80,11 +80,8 @@ public class StorageHelper {
|
||||
File storage = new File("/storage/");
|
||||
if (storage.exists() && storage.isDirectory()) {
|
||||
String mounts = null;
|
||||
try {
|
||||
Scanner scanner = new Scanner(new File("/proc/mounts"));
|
||||
try (Scanner scanner = new Scanner(new File("/proc/mounts"))) {
|
||||
mounts = scanner.useDelimiter("\\A").next();
|
||||
scanner.close();
|
||||
//Log.e("Mounts",mounts);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -116,9 +113,7 @@ public class StorageHelper {
|
||||
//Legacy code for Android < 4.0 that still didn't have /storage
|
||||
|
||||
ArrayList<String> entries = new ArrayList<>();
|
||||
BufferedReader buf_reader = null;
|
||||
try {
|
||||
buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
|
||||
try (BufferedReader buf_reader = new BufferedReader(new FileReader("/proc/mounts"))){
|
||||
String entry;
|
||||
while ((entry = buf_reader.readLine()) != null) {
|
||||
//Log.e("getStorageList", entry);
|
||||
@ -129,13 +124,6 @@ public class StorageHelper {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (buf_reader != null) {
|
||||
try {
|
||||
buf_reader.close();
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (String line : entries) {
|
||||
|
@ -82,8 +82,7 @@ class AppDatabase {
|
||||
|
||||
void setEnabled(String packageName, boolean isEnabled) {
|
||||
String[] columns = new String[]{KEY_IS_ENABLED};
|
||||
Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null);
|
||||
try {
|
||||
try (Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null)) {
|
||||
ContentValues cv = new ContentValues();
|
||||
cv.put(KEY_IS_ENABLED, isEnabled ? "true" : "false");
|
||||
if (res.getCount() > 0) {
|
||||
@ -92,8 +91,6 @@ class AppDatabase {
|
||||
cv.put(KEY_PACKAGE_NAME, packageName);
|
||||
ourDatabase.insert(DATABASE_TABLE, null, cv);
|
||||
}
|
||||
} finally {
|
||||
res.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,8 +100,7 @@ class AppDatabase {
|
||||
|
||||
boolean isEnabled(String packageName) {
|
||||
String[] columns = new String[]{KEY_IS_ENABLED};
|
||||
Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null);
|
||||
try {
|
||||
try (Cursor res = ourDatabase.query(DATABASE_TABLE, columns, KEY_PACKAGE_NAME + " =? ", new String[]{packageName}, null, null, null)) {
|
||||
boolean result;
|
||||
if (res.getCount() > 0) {
|
||||
res.moveToFirst();
|
||||
@ -113,8 +109,6 @@ class AppDatabase {
|
||||
result = getDefaultStatus(packageName);
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
res.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,8 @@ import org.kde.kdeconnect_tp.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
class ShareNotification {
|
||||
|
||||
@ -104,14 +106,14 @@ class ShareNotification {
|
||||
|
||||
//If it's an image, try to show it in the notification
|
||||
if (mimeType.startsWith("image/")) {
|
||||
try {
|
||||
Bitmap image = BitmapFactory.decodeStream(device.getContext().getContentResolver().openInputStream(destinationUri));
|
||||
try (InputStream inputStream = device.getContext().getContentResolver().openInputStream(destinationUri)) {
|
||||
Bitmap image = BitmapFactory.decodeStream(inputStream);
|
||||
if (image != null) {
|
||||
builder.setLargeIcon(image);
|
||||
builder.setStyle(new NotificationCompat.BigPictureStyle()
|
||||
.bigPicture(image));
|
||||
}
|
||||
} catch (FileNotFoundException ignored) {
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (!"file".equals(destinationUri.getScheme())) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user