|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.background.healthreport; |
|
6 |
|
7 import java.io.File; |
|
8 import java.util.HashMap; |
|
9 |
|
10 import org.mozilla.gecko.background.common.log.Logger; |
|
11 |
|
12 import android.content.Context; |
|
13 |
|
14 /** |
|
15 * Manages a set of per-profile Health Report storage helpers. |
|
16 */ |
|
17 public class HealthReportDatabases { |
|
18 private static final String LOG_TAG = "HealthReportDatabases"; |
|
19 |
|
20 private Context context; |
|
21 private final HashMap<File, HealthReportDatabaseStorage> storages = new HashMap<File, HealthReportDatabaseStorage>(); |
|
22 |
|
23 |
|
24 public HealthReportDatabases(final Context context) { |
|
25 this.context = context; |
|
26 } |
|
27 |
|
28 public synchronized HealthReportDatabaseStorage getDatabaseHelperForProfile(final File profileDir) { |
|
29 if (profileDir == null) { |
|
30 throw new IllegalArgumentException("No profile provided."); |
|
31 } |
|
32 |
|
33 if (this.storages.containsKey(profileDir)) { |
|
34 return this.storages.get(profileDir); |
|
35 } |
|
36 |
|
37 final HealthReportDatabaseStorage helper; |
|
38 helper = new HealthReportDatabaseStorage(this.context, profileDir); |
|
39 this.storages.put(profileDir, helper); |
|
40 return helper; |
|
41 } |
|
42 |
|
43 public synchronized void closeDatabaseHelpers() { |
|
44 for (HealthReportDatabaseStorage helper : storages.values()) { |
|
45 try { |
|
46 helper.close(); |
|
47 } catch (Exception e) { |
|
48 Logger.warn(LOG_TAG, "Failed to close database helper.", e); |
|
49 } |
|
50 } |
|
51 storages.clear(); |
|
52 } |
|
53 } |