blob: 26d20d151b8c3932aa643889279746e081385042 [file] [log] [blame]
Wenyi Wang142a3442016-02-04 14:08:45 -08001/*
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 */
16package com.android.contacts.common.vcard;
17
18import android.content.ComponentName;
19import android.net.Uri;
20import android.os.IBinder;
21import android.support.v4.content.FileProvider;
22import android.util.Log;
23
24import com.android.contacts.common.R;
Walter Jang3a0b4832016-10-12 11:02:54 -070025import com.android.contactsbind.FeedbackHelper;
Wenyi Wang142a3442016-02-04 14:08:45 -080026
27import java.io.File;
28import java.io.IOException;
Wenyi Wangb81f6eb2016-03-02 11:35:36 -080029import java.text.SimpleDateFormat;
30import java.util.Date;
31import java.util.Locale;
Wenyi Wang142a3442016-02-04 14:08:45 -080032
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 */
37public class ShareVCardActivity extends ExportVCardActivity {
38 private static final String LOG_TAG = "VCardShare";
Wenyi Wangb81f6eb2016-03-02 11:35:36 -080039 private final String EXPORT_FILE_PREFIX = "vcards_";
Wenyi Wang142a3442016-02-04 14:08:45 -080040 private final long A_DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
41
42 @Override
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) {
Walter Jang3a0b4832016-10-12 11:02:54 -070054 FeedbackHelper.sendFeedback(this, LOG_TAG, "Failed to create .vcf file", e);
guanxiongliucca57302016-06-08 18:41:59 -070055 finish();
Wenyi Wang142a3442016-02-04 14:08:45 -080056 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));
guanxiongliucca57302016-06-08 18:41:59 -070067 finish();
Wenyi Wang142a3442016-02-04 14:08:45 -080068 }
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 */
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
87 private File getLocalFile() {
Wenyi Wangb81f6eb2016-03-02 11:35:36 -080088 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);
Wenyi Wang142a3442016-02-04 14:08:45 -080092 }
93}