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