summaryrefslogtreecommitdiffstats
path: root/src/com/android/server/telecom/Blacklist.java
blob: 5584e6d025e65be08c546de21601dc3fb0644d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.android.server.telecom;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.provider.Telephony;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.util.HashSet;

/**
 * This class used to handle the blacklist data. Its
 * only remaining purpose is legacy data migration
 */
class Blacklist {
    private static class PhoneNumber implements Externalizable {
        static final long serialVersionUID = 32847013274L;
        String phone;

        public PhoneNumber() {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            phone = (String) in.readObject();
        }

        @Override
        public int hashCode() {
            return phone != null ? phone.hashCode() : 0;
        }
    }

    private static final String BLFILE = "blacklist.dat";
    private static final int BLFILE_VER = 1;

    public static void migrateOldDataIfPresent(Context context) {
        ObjectInputStream ois = null;
        HashSet<PhoneNumber> data = null;

        try {
            ois = new ObjectInputStream(context.openFileInput(BLFILE));
            Object o = ois.readObject();
            if (o != null && o instanceof Integer) {
                // check the version
                Integer version = (Integer) o;
                if (version == BLFILE_VER) {
                    Object numbers = ois.readObject();
                    if (numbers instanceof HashSet) {
                        data = (HashSet<PhoneNumber>) numbers;
                    }
                }
            }
        } catch (IOException e) {
            // Do nothing
        } catch (ClassNotFoundException e) {
            // Do nothing
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    // Do nothing
                }
                context.deleteFile(BLFILE);
            }
        }
        if (data != null) {
            ContentResolver cr = context.getContentResolver();
            ContentValues cv = new ContentValues();
            cv.put(Telephony.Blacklist.PHONE_MODE, 1);

            for (PhoneNumber number : data) {
                Uri uri = Uri.withAppendedPath(
                        Telephony.Blacklist.CONTENT_FILTER_BYNUMBER_URI, number.phone);
                cv.put(Telephony.Blacklist.NUMBER, number.phone);
                cr.update(uri, cv, null, null);
            }
        }
    }
}