Improve SyncManager.getDeviceIdInternal()

- Don't swallow exception information.
- If the file exists but can't read, try to remove it.
- Don't use createNewFile.  Just overwrite.

Bug 2898372

Change-Id: I7c0802f751a020c546aa07fa932b41ead31a8dc8
This commit is contained in:
Makoto Onuki 2010-08-05 16:03:05 -07:00
parent 191448b430
commit 408de8e4c5

View File

@ -1053,31 +1053,34 @@ public class SyncManager extends Service implements Runnable {
context = INSTANCE; context = INSTANCE;
} }
try { File f = context.getFileStreamPath("deviceName");
File f = context.getFileStreamPath("deviceName"); BufferedReader rdr = null;
BufferedReader rdr = null; String id;
String id; if (f.exists()) {
if (f.exists() && f.canRead()) { if (f.canRead()) {
rdr = new BufferedReader(new FileReader(f), 128); rdr = new BufferedReader(new FileReader(f), 128);
id = rdr.readLine(); id = rdr.readLine();
rdr.close(); rdr.close();
return id; return id;
} else if (f.createNewFile()) { } else {
BufferedWriter w = new BufferedWriter(new FileWriter(f), 128); Log.w(Email.LOG_TAG, f.getAbsolutePath() + ": File exists, but can't read?" +
final String consistentDeviceId = Utility.getConsistentDeviceId(context); " Trying to remove.");
if (consistentDeviceId != null) { if (!f.delete()) {
// Use different prefix from random IDs. Log.w(Email.LOG_TAG, "Remove failed. Tring to overwrite.");
id = "androidc" + consistentDeviceId;
} else {
id = "android" + System.currentTimeMillis();
} }
w.write(id);
w.close();
return id;
} }
} catch (IOException e) {
} }
throw new IOException("Can't get device name"); BufferedWriter w = new BufferedWriter(new FileWriter(f), 128);
final String consistentDeviceId = Utility.getConsistentDeviceId(context);
if (consistentDeviceId != null) {
// Use different prefix from random IDs.
id = "androidc" + consistentDeviceId;
} else {
id = "android" + System.currentTimeMillis();
}
w.write(id);
w.close();
return id;
} }
@Override @Override
@ -1683,7 +1686,7 @@ public class SyncManager extends Service implements Runnable {
getDeviceId(this); getDeviceId(this);
} catch (IOException e) { } catch (IOException e) {
// We can't run in this situation // We can't run in this situation
throw new RuntimeException(); throw new RuntimeException(e);
} }
} }
// Run the reconciler and clean up any mismatched accounts - if we weren't running when // Run the reconciler and clean up any mismatched accounts - if we weren't running when