1 /* 2 * Copyright 2018 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.settingslib.media; 17 18 import android.content.Context; 19 import android.content.SharedPreferences; 20 21 /** 22 * ConnectionRecordManager represents the sharedPreferences operation on device usage record 23 */ 24 public class ConnectionRecordManager { 25 private static final Object sInstanceSync = new Object(); 26 private static final String KEY_LAST_SELECTED_DEVICE = "last_selected_device"; 27 private static final String SHARED_PREFERENCES_NAME = "seamless_transfer_record"; 28 private static final String TAG = "ConnectionRecordManager"; 29 private static ConnectionRecordManager sInstance; 30 31 private String mLastSelectedDevice; 32 33 /** 34 * Get an {@code ConnectionRecordManager} instance (create one if necessary). 35 */ getInstance()36 public static ConnectionRecordManager getInstance() { 37 synchronized (sInstanceSync) { 38 if (sInstance == null) { 39 sInstance = new ConnectionRecordManager(); 40 } 41 } 42 return sInstance; 43 } 44 getSharedPreferences(Context context)45 private SharedPreferences getSharedPreferences(Context context) { 46 return context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); 47 } 48 49 /** 50 * Get connection record from sharedPreferences 51 * 52 * @param id a unique device Id 53 * @return the the usage result 54 */ fetchConnectionRecord(Context context, String id)55 public synchronized int fetchConnectionRecord(Context context, String id) { 56 return getSharedPreferences(context).getInt(id, 0); 57 } 58 59 /** 60 * Get the last selected device from sharedPreferences 61 */ fetchLastSelectedDevice(Context context)62 public synchronized void fetchLastSelectedDevice(Context context) { 63 mLastSelectedDevice = getSharedPreferences(context).getString(KEY_LAST_SELECTED_DEVICE, 64 null); 65 } 66 67 /** 68 * Set device usage time and last selected device in sharedPreference 69 * 70 * @param id a unique device Id 71 * @param record usage times 72 */ setConnectionRecord(Context context, String id, int record)73 public synchronized void setConnectionRecord(Context context, String id, int record) { 74 final SharedPreferences.Editor editor = getSharedPreferences(context).edit(); 75 // Update used times 76 mLastSelectedDevice = id; 77 editor.putInt(mLastSelectedDevice, record); 78 // Update last used device 79 editor.putString(KEY_LAST_SELECTED_DEVICE, mLastSelectedDevice); 80 editor.apply(); 81 } 82 83 /** 84 * @return the last selected device 85 */ getLastSelectedDevice()86 public synchronized String getLastSelectedDevice() { 87 return mLastSelectedDevice; 88 } 89 } 90