• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.contacts.common.vcard;
17 
18 import android.content.ComponentName;
19 import android.net.Uri;
20 import android.os.IBinder;
21 import android.support.v4.content.FileProvider;
22 import android.util.Log;
23 
24 import com.android.contacts.common.R;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Locale;
31 
32 /**
33  * This activity connects to VCardService, creates a .vcf file in cache directory and send export
34  * request with the file URI so as to write contacts data to the file in background.
35  */
36 public class ShareVCardActivity extends ExportVCardActivity {
37     private static final String LOG_TAG = "VCardShare";
38     private final String EXPORT_FILE_PREFIX = "vcards_";
39     private final long A_DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
40 
41     @Override
onServiceConnected(ComponentName name, IBinder binder)42     public synchronized void onServiceConnected(ComponentName name, IBinder binder) {
43         if (DEBUG) Log.d(LOG_TAG, "connected to service, requesting a destination file name");
44         mConnected = true;
45         mService = ((VCardService.MyBinder) binder).getService();
46 
47         clearExportFiles();
48 
49         final File file = getLocalFile();
50         try {
51             file.createNewFile();
52         } catch (IOException e) {
53             Log.e(LOG_TAG, "Failed to create .vcf file, because: " + e);
54             unbindAndFinish();
55             return;
56         }
57 
58         final Uri contentUri = FileProvider.getUriForFile(this,
59                 getString(R.string.contacts_file_provider_authority), file);
60         if (DEBUG) Log.d(LOG_TAG, "exporting to " + contentUri);
61 
62         final ExportRequest request = new ExportRequest(contentUri);
63         // The connection object will call finish().
64         mService.handleExportRequest(request, new NotificationImportExportListener(
65                 ShareVCardActivity.this));
66         unbindAndFinish();
67     }
68 
69     /**
70      * Delete the files (that are untouched for more than 1 day) in the cache directory.
71      * We cannot rely on VCardService to delete export files because it will delete export files
72      * right after finishing writing so no files could be shared. Therefore, our approach to
73      * deleting export files is:
74      * 1. put export files in cache directory so that Android may delete them;
75      * 2. manually delete the files that are older than 1 day when service is connected.
76      */
clearExportFiles()77     private void clearExportFiles() {
78         for (File file : getCacheDir().listFiles()) {
79             final long ageInMillis = System.currentTimeMillis() - file.lastModified();
80             if (file.getName().startsWith(EXPORT_FILE_PREFIX) && ageInMillis > A_DAY_IN_MILLIS) {
81                 file.delete();
82             }
83         }
84     }
85 
getLocalFile()86     private File getLocalFile() {
87         final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
88         final String currentDateString = dateFormat.format(new Date()).toString();
89         final String localFilename = EXPORT_FILE_PREFIX + currentDateString + ".vcf";
90         return new File(getCacheDir(), localFilename);
91     }
92 }