Proyectos de Subversion Android Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
package com.cesams.twogetskills.library;
2
 
3
import java.io.File;
4
import java.io.FileOutputStream;
5
import java.io.IOException;
6
import java.io.RandomAccessFile;
7
import java.util.UUID;
8
 
9
import android.content.Context;
10
 
11
public class UniqueID {
12
    private static String sID = null;
13
    private static final String INSTALLATION = "TwoGetSkillsID.dat";
14
 
15
    public synchronized static String id(Context context) {
16
        if (sID == null) {
17
            File installation = new File(context.getFilesDir(), INSTALLATION);
18
            try {
19
                if (!installation.exists())
20
                    writeInstallationFile(installation);
21
                sID = readInstallationFile(installation);
22
            } catch (Exception e) {
23
                throw new RuntimeException(e);
24
            }
25
        }
26
        return sID;
27
    }
28
 
29
    private static String readInstallationFile(File installation) throws IOException {
30
        RandomAccessFile f = new RandomAccessFile(installation, "r");
31
        byte[] bytes = new byte[(int) f.length()];
32
        f.readFully(bytes);
33
        f.close();
34
        return new String(bytes);
35
    }
36
 
37
    private static void writeInstallationFile(File installation) throws IOException {
38
        FileOutputStream out = new FileOutputStream(installation);
39
        String id = UUID.randomUUID().toString();
40
        out.write(id.getBytes());
41
        out.close();
42
    }
43
}