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