PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/src/com/android/phone/sip/SipProfileDb.java

https://bitbucket.org/miragerom/android_packages_apps_phone
Java | 144 lines | 109 code | 17 blank | 18 comment | 15 complexity | 3c5c52916e9b38d6905d5284ae22050d MD5 | raw file
  1/*
  2 * Copyright (C) 2010 The Android Open Source Project
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.android.phone.sip;
 18
 19import com.android.internal.os.AtomicFile;
 20
 21import android.content.Context;
 22import android.net.sip.SipProfile;
 23import android.util.Log;
 24
 25import java.io.File;
 26import java.io.FileInputStream;
 27import java.io.FileOutputStream;
 28import java.io.IOException;
 29import java.io.ObjectInputStream;
 30import java.io.ObjectOutputStream;
 31import java.util.ArrayList;
 32import java.util.Collections;
 33import java.util.List;
 34
 35/**
 36 * Utility class that helps perform operations on the SipProfile database.
 37 */
 38public class SipProfileDb {
 39    private static final String TAG = SipProfileDb.class.getSimpleName();
 40
 41    private static final String PROFILES_DIR = "/profiles/";
 42    private static final String PROFILE_OBJ_FILE = ".pobj";
 43
 44    private String mProfilesDirectory;
 45    private SipSharedPreferences mSipSharedPreferences;
 46    private int mProfilesCount = -1;
 47
 48    public SipProfileDb(Context context) {
 49        mProfilesDirectory = context.getFilesDir().getAbsolutePath()
 50                + PROFILES_DIR;
 51        mSipSharedPreferences = new SipSharedPreferences(context);
 52    }
 53
 54    public void deleteProfile(SipProfile p) {
 55        synchronized(SipProfileDb.class) {
 56            deleteProfile(new File(mProfilesDirectory + p.getProfileName()));
 57            if (mProfilesCount < 0) retrieveSipProfileListInternal();
 58            mSipSharedPreferences.setProfilesCount(--mProfilesCount);
 59        }
 60    }
 61
 62    private void deleteProfile(File file) {
 63        if (file.isDirectory()) {
 64            for (File child : file.listFiles()) deleteProfile(child);
 65        }
 66        file.delete();
 67    }
 68
 69    public void saveProfile(SipProfile p) throws IOException {
 70        synchronized(SipProfileDb.class) {
 71            if (mProfilesCount < 0) retrieveSipProfileListInternal();
 72            File f = new File(mProfilesDirectory + p.getProfileName());
 73            if (!f.exists()) f.mkdirs();
 74            AtomicFile atomicFile =
 75                    new AtomicFile(new File(f, PROFILE_OBJ_FILE));
 76            FileOutputStream fos = null;
 77            ObjectOutputStream oos = null;
 78            try {
 79                fos = atomicFile.startWrite();
 80                oos = new ObjectOutputStream(fos);
 81                oos.writeObject(p);
 82                oos.flush();
 83                mSipSharedPreferences.setProfilesCount(++mProfilesCount);
 84                atomicFile.finishWrite(fos);
 85            } catch (IOException e) {
 86                atomicFile.failWrite(fos);
 87                throw e;
 88            } finally {
 89                if (oos != null) oos.close();
 90            }
 91        }
 92    }
 93
 94    public int getProfilesCount() {
 95        return (mProfilesCount < 0) ?
 96                mSipSharedPreferences.getProfilesCount() : mProfilesCount;
 97    }
 98
 99    public List<SipProfile> retrieveSipProfileList() {
100        synchronized(SipProfileDb.class) {
101            return retrieveSipProfileListInternal();
102        }
103    }
104
105    private List<SipProfile> retrieveSipProfileListInternal() {
106        List<SipProfile> sipProfileList = Collections.synchronizedList(
107                new ArrayList<SipProfile>());
108
109        File root = new File(mProfilesDirectory);
110        String[] dirs = root.list();
111        if (dirs == null) return sipProfileList;
112        for (String dir : dirs) {
113            File f = new File(new File(root, dir), PROFILE_OBJ_FILE);
114            if (!f.exists()) continue;
115            try {
116                SipProfile p = deserialize(f);
117                if (p == null) continue;
118                if (!dir.equals(p.getProfileName())) continue;
119
120                sipProfileList.add(p);
121            } catch (IOException e) {
122                Log.e(TAG, "retrieveProfileListFromStorage()", e);
123            }
124        }
125        mProfilesCount = sipProfileList.size();
126        mSipSharedPreferences.setProfilesCount(mProfilesCount);
127        return sipProfileList;
128    }
129
130    private SipProfile deserialize(File profileObjectFile) throws IOException {
131        AtomicFile atomicFile = new AtomicFile(profileObjectFile);
132        ObjectInputStream ois = null;
133        try {
134            ois = new ObjectInputStream(atomicFile.openRead());
135            SipProfile p = (SipProfile) ois.readObject();
136            return p;
137        } catch (ClassNotFoundException e) {
138            Log.w(TAG, "deserialize a profile: " + e);
139        } finally {
140            if (ois!= null) ois.close();
141        }
142        return null;
143    }
144}