/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. package com.android.phone.sip;
  17. import com.android.internal.os.AtomicFile;
  18. import android.content.Context;
  19. import android.net.sip.SipProfile;
  20. import android.util.Log;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.ObjectInputStream;
  26. import java.io.ObjectOutputStream;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.List;
  30. /**
  31. * Utility class that helps perform operations on the SipProfile database.
  32. */
  33. public class SipProfileDb {
  34. private static final String TAG = SipProfileDb.class.getSimpleName();
  35. private static final String PROFILES_DIR = "/profiles/";
  36. private static final String PROFILE_OBJ_FILE = ".pobj";
  37. private String mProfilesDirectory;
  38. private SipSharedPreferences mSipSharedPreferences;
  39. private int mProfilesCount = -1;
  40. public SipProfileDb(Context context) {
  41. mProfilesDirectory = context.getFilesDir().getAbsolutePath()
  42. + PROFILES_DIR;
  43. mSipSharedPreferences = new SipSharedPreferences(context);
  44. }
  45. public void deleteProfile(SipProfile p) {
  46. synchronized(SipProfileDb.class) {
  47. deleteProfile(new File(mProfilesDirectory + p.getProfileName()));
  48. if (mProfilesCount < 0) retrieveSipProfileListInternal();
  49. mSipSharedPreferences.setProfilesCount(--mProfilesCount);
  50. }
  51. }
  52. private void deleteProfile(File file) {
  53. if (file.isDirectory()) {
  54. for (File child : file.listFiles()) deleteProfile(child);
  55. }
  56. file.delete();
  57. }
  58. public void saveProfile(SipProfile p) throws IOException {
  59. synchronized(SipProfileDb.class) {
  60. if (mProfilesCount < 0) retrieveSipProfileListInternal();
  61. File f = new File(mProfilesDirectory + p.getProfileName());
  62. if (!f.exists()) f.mkdirs();
  63. AtomicFile atomicFile =
  64. new AtomicFile(new File(f, PROFILE_OBJ_FILE));
  65. FileOutputStream fos = null;
  66. ObjectOutputStream oos = null;
  67. try {
  68. fos = atomicFile.startWrite();
  69. oos = new ObjectOutputStream(fos);
  70. oos.writeObject(p);
  71. oos.flush();
  72. mSipSharedPreferences.setProfilesCount(++mProfilesCount);
  73. atomicFile.finishWrite(fos);
  74. } catch (IOException e) {
  75. atomicFile.failWrite(fos);
  76. throw e;
  77. } finally {
  78. if (oos != null) oos.close();
  79. }
  80. }
  81. }
  82. public int getProfilesCount() {
  83. return (mProfilesCount < 0) ?
  84. mSipSharedPreferences.getProfilesCount() : mProfilesCount;
  85. }
  86. public List<SipProfile> retrieveSipProfileList() {
  87. synchronized(SipProfileDb.class) {
  88. return retrieveSipProfileListInternal();
  89. }
  90. }
  91. private List<SipProfile> retrieveSipProfileListInternal() {
  92. List<SipProfile> sipProfileList = Collections.synchronizedList(
  93. new ArrayList<SipProfile>());
  94. File root = new File(mProfilesDirectory);
  95. String[] dirs = root.list();
  96. if (dirs == null) return sipProfileList;
  97. for (String dir : dirs) {
  98. File f = new File(new File(root, dir), PROFILE_OBJ_FILE);
  99. if (!f.exists()) continue;
  100. try {
  101. SipProfile p = deserialize(f);
  102. if (p == null) continue;
  103. if (!dir.equals(p.getProfileName())) continue;
  104. sipProfileList.add(p);
  105. } catch (IOException e) {
  106. Log.e(TAG, "retrieveProfileListFromStorage()", e);
  107. }
  108. }
  109. mProfilesCount = sipProfileList.size();
  110. mSipSharedPreferences.setProfilesCount(mProfilesCount);
  111. return sipProfileList;
  112. }
  113. private SipProfile deserialize(File profileObjectFile) throws IOException {
  114. AtomicFile atomicFile = new AtomicFile(profileObjectFile);
  115. ObjectInputStream ois = null;
  116. try {
  117. ois = new ObjectInputStream(atomicFile.openRead());
  118. SipProfile p = (SipProfile) ois.readObject();
  119. return p;
  120. } catch (ClassNotFoundException e) {
  121. Log.w(TAG, "deserialize a profile: " + e);
  122. } finally {
  123. if (ois!= null) ois.close();
  124. }
  125. return null;
  126. }
  127. }