├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── dictionaries
│ └── abhay_yadav.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── Screenshots
├── Screenshot1.png
└── Screenshot2.png
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── aarsy
│ │ └── github
│ │ └── com
│ │ └── ez_vcard_android
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── aarsy
│ │ │ └── github
│ │ │ └── com
│ │ │ └── ez_vcard_android
│ │ │ ├── MyAlertDialog.java
│ │ │ ├── VCFFiles.java
│ │ │ └── VCFfilesListActivity.java
│ └── res
│ │ ├── drawable-v21
│ │ └── recycler_view_item_background.xml
│ │ ├── drawable
│ │ └── recycler_view_item_background.xml
│ │ ├── layout
│ │ ├── activity_vcffiles_list.xml
│ │ ├── custom_dialog_layout.xml
│ │ └── single_item_vcflist.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── aarsy
│ └── github
│ └── com
│ └── ez_vcard_android
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── vcard-androidParser
├── .gitignore
├── build.gradle
└── src
└── main
├── AndroidManifest.xml
├── java
└── ezvcard
│ └── android
│ ├── AndroidCustomField.java
│ ├── AndroidCustomFieldScribe.java
│ ├── ContactOperations.java
│ └── DataMappings.java
├── res
├── values-w820dp
│ └── dimens.xml
└── values
│ ├── dimens.xml
│ └── strings.xml
└── resources
└── ez-vcard-android.license
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/dictionaries/abhay_yadav.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | Android
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | 1.8
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vcard-androidParser
2 |
3 | This is an android library which converts [ez-vcard](http://github.com/mangstadt/ez-vcard) `VCard` object into appropriate Android compatible version so that it can be used to read vcard files easily in android platform
4 |
5 | ------
6 |
7 | # Download
8 | ### Using Gradle- under dependencies section:
9 |
10 | compile 'com.github.aarsy.vcard-androidParser:vcard-androidParser:1.0.0'
11 |
12 | ### or Using Maven:
13 |
14 |
15 | com.github.aarsy.vcard-androidParser
16 | vcard-androidParser
17 | 1.0.0
18 | pom
19 |
20 |
21 | ------
22 |
23 | # Documentation
24 |
25 | ### Reading a vcard or vcf file
26 |
27 | File vcardFile = new File(filePath);
28 | VCardReader reader = null;
29 | try {
30 | reader = new VCardReader(vcardFile);
31 | reader.registerScribe(new AndroidCustomFieldScribe());
32 |
33 | ContactOperations operations = new ContactOperations(context, account_name, account_type);
34 |
35 | //insert contacts with specific account_name and their types. For example:
36 | //both account_name=null and account_type=null if you want to insert contacts into phone
37 | //you can also pass other accounts
38 |
39 | VCard vcard = null;
40 | while ((vcard = reader.readNext()) != null) {
41 | operations.insertContact(vcard);
42 | }
43 | }catch (Exception e) {
44 | e.printStackTrace();
45 | }finally {
46 | closeQuietly(reader);
47 | }
48 |
49 | See sample for more details..
50 |
51 | ------
52 |
53 | # Sample Screenshots
54 |
|
55 |
[vcard-androidParser sample app](https://play.google.com/store/apps/details?id=aarsy.gitbub.com.ez_vcard_android)
56 | ------
57 |
58 | # Compatibility
59 |
60 | **Minimum Android SDK**: This library requires a minimum API level of **10**.
61 |
62 | # Applications already using this library
63 |
64 |
[vcfToSIMCard](https://play.google.com/store/apps/details?id=app.aarsy.vcftosimcard)
65 |
66 | # Special thanks to
67 |
68 | [Mike Angstadt](https://github.com/mangstadt)
69 |
70 |
--------------------------------------------------------------------------------
/Screenshots/Screenshot1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aarsy/vcard-androidParser/6a417126770a6b51b6ec921c56aac6916c023d39/Screenshots/Screenshot1.png
--------------------------------------------------------------------------------
/Screenshots/Screenshot2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aarsy/vcard-androidParser/6a417126770a6b51b6ec921c56aac6916c023d39/Screenshots/Screenshot2.png
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "aarsy.gitbub.com.ez_vcard_android"
8 | minSdkVersion 13
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.1.0'
28 | compile 'com.android.support:support-v4:25.1.0'
29 | compile 'com.android.support:design:25.1.0'
30 | compile 'com.android.support:recyclerview-v7:25.1.0'
31 | compile 'com.android.support:support-v13:25.1.0'
32 | compile project(':vcard-androidParser')
33 | testCompile 'junit:junit:4.12'
34 | }
35 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/aarsy/github/com/ez_vcard_android/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package aarsy.gitbub.com.ez_vcard_android;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static junit.framework.Assert.assertEquals;
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumentation test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() throws Exception {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getTargetContext();
24 |
25 | assertEquals("aarsy.gitbub.com.ez_vcard_android", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
15 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/java/aarsy/github/com/ez_vcard_android/MyAlertDialog.java:
--------------------------------------------------------------------------------
1 | package aarsy.github.com.ez_vcard_android;
2 |
3 | import android.graphics.Point;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.DialogFragment;
7 | import android.view.Display;
8 | import android.view.Gravity;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.view.Window;
13 | import android.view.WindowManager;
14 | import android.widget.LinearLayout;
15 | import android.widget.TextView;
16 |
17 | public class MyAlertDialog extends DialogFragment implements View.OnClickListener {
18 |
19 | private String accountType;
20 | private String accountName;
21 |
22 | public interface ItemAccountSelectedListener {
23 | void onAccountSelected(String account_name, String account_type);
24 | }
25 |
26 | public static MyAlertDialog newInstance(String account_name, String account_type) {
27 | Bundle args = new Bundle();
28 | MyAlertDialog fragment = new MyAlertDialog();
29 | args.putString("accountName", account_name);
30 | args.putString("accountType", account_type);
31 | fragment.setArguments(args);
32 | return fragment;
33 | }
34 |
35 | public void onResume() {
36 | // Store access variables for window and blank point
37 | Window window = getDialog().getWindow();
38 | Point size = new Point();
39 | // Store dimensions of the screen in `size`
40 | Display display = window.getWindowManager().getDefaultDisplay();
41 | display.getSize(size);
42 | // Set the width of the dialog proportional to 75% of the screen width
43 | window.setLayout((int) (size.x * 0.85), WindowManager.LayoutParams.WRAP_CONTENT);
44 | window.setGravity(Gravity.CENTER);
45 | // Call super onResume after sizing
46 | super.onResume();
47 | }
48 |
49 | @Nullable
50 | @Override
51 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
52 | return inflater.inflate(R.layout.custom_dialog_layout, container);
53 | }
54 |
55 | @Override
56 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
57 | super.onViewCreated(view, savedInstanceState);
58 | ((LinearLayout) view.findViewById(R.id.ll_google_account)).setOnClickListener(this);
59 | ((LinearLayout) view.findViewById(R.id.ll_simcard1)).setOnClickListener(this);
60 | ((LinearLayout) view.findViewById(R.id.ll_device)).setOnClickListener(this);
61 | TextView tv_mail_id = (TextView) view.findViewById(R.id.tv_email_id);
62 | accountName = getArguments().getString("accountName");
63 | accountType = getArguments().getString("accountType");
64 | tv_mail_id.setText(accountName);
65 | getDialog().setTitle("Save to");
66 | }
67 |
68 | @Override
69 | public void onClick(View v) {
70 | ItemAccountSelectedListener listener = (ItemAccountSelectedListener) getActivity();
71 | switch (v.getId()) {
72 | case R.id.ll_google_account:
73 | listener.onAccountSelected(accountName, accountType);
74 | dismiss();
75 | break;
76 | case R.id.ll_simcard1:
77 | listener.onAccountSelected("SIMCARD", null);
78 | dismiss();
79 | break;
80 | case R.id.ll_device:
81 | listener.onAccountSelected("Phone", null);
82 | dismiss();
83 | break;
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/java/aarsy/github/com/ez_vcard_android/VCFFiles.java:
--------------------------------------------------------------------------------
1 | package aarsy.github.com.ez_vcard_android;
2 |
3 | /**
4 | * Created by abhay yadav on 01-Jan-17.
5 | */
6 | public class VCFFiles {
7 | int fileId;
8 | String fileName;
9 | String filePath;
10 |
11 | public VCFFiles(int file_id, String file_name, String file_fullpath) {
12 | this.fileId=file_id;
13 | this.fileName=file_name;
14 | this.filePath=file_fullpath;
15 | }
16 |
17 | public String getFilePath() {
18 | return filePath;
19 | }
20 |
21 | public void setFilePath(String filePath) {
22 | this.filePath = filePath;
23 | }
24 |
25 | public String getFileName() {
26 | return fileName;
27 | }
28 |
29 | public void setFileName(String fileName) {
30 | this.fileName = fileName;
31 | }
32 | }
--------------------------------------------------------------------------------
/app/src/main/java/aarsy/github/com/ez_vcard_android/VCFfilesListActivity.java:
--------------------------------------------------------------------------------
1 | package aarsy.github.com.ez_vcard_android;
2 |
3 | import android.Manifest;
4 | import android.accounts.Account;
5 | import android.accounts.AccountManager;
6 | import android.app.Activity;
7 | import android.app.Dialog;
8 | import android.app.ProgressDialog;
9 | import android.content.ContentValues;
10 | import android.content.Context;
11 | import android.content.DialogInterface;
12 | import android.content.OperationApplicationException;
13 | import android.content.pm.PackageManager;
14 | import android.database.Cursor;
15 | import android.net.Uri;
16 | import android.os.AsyncTask;
17 | import android.os.Bundle;
18 | import android.os.RemoteException;
19 | import android.provider.MediaStore;
20 | import android.support.annotation.NonNull;
21 | import android.support.annotation.Nullable;
22 | import android.support.design.widget.Snackbar;
23 | import android.support.v4.app.ActivityCompat;
24 | import android.support.v4.app.FragmentManager;
25 | import android.support.v4.app.LoaderManager;
26 | import android.support.v4.content.ContextCompat;
27 | import android.support.v4.content.CursorLoader;
28 | import android.support.v4.content.Loader;
29 | import android.support.v7.app.ActionBar;
30 | import android.support.v7.app.AlertDialog;
31 | import android.support.v7.app.AppCompatActivity;
32 | import android.support.v7.widget.LinearLayoutManager;
33 | import android.support.v7.widget.RecyclerView;
34 | import android.util.Log;
35 | import android.view.LayoutInflater;
36 | import android.view.View;
37 | import android.view.ViewGroup;
38 | import android.widget.RelativeLayout;
39 | import android.widget.TextView;
40 | import android.widget.Toast;
41 |
42 | import java.io.File;
43 | import java.util.ArrayList;
44 | import java.util.List;
45 |
46 | import ezvcard.VCard;
47 | import ezvcard.android.AndroidCustomFieldScribe;
48 | import ezvcard.android.ContactOperations;
49 | import ezvcard.io.text.VCardReader;
50 |
51 | import static ezvcard.util.IOUtils.closeQuietly;
52 |
53 |
54 | /**
55 | * Created by abhay yadav on 10-Jan-17.
56 | */
57 | public class VCFfilesListActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks, MyAlertDialog.ItemAccountSelectedListener {
58 |
59 | private static final String TAG = "VCFfilesListActivity";
60 | private static final int DIALOG_COPY = 1;
61 | private VCFFilesAdapter adapter;
62 | private RecyclerView recyclerView;
63 | private List allFilesList;
64 | private Context context;
65 | private static final int LOADER_ID = 1;
66 | private int selectedFilePositioninList = 0;
67 | private RelativeLayout rlNoContacts;
68 | private final int REQUEST_CODE_PERMISSION_READ_EXT_STORAGE = 100;
69 | private final int REQUEST_CODE_PERMISSION_WRITE_CONTACTS = 101;
70 | private String filePath;
71 |
72 | @Override
73 | protected void onCreate(@Nullable Bundle savedInstanceState) {
74 | super.onCreate(savedInstanceState);
75 | setContentView(R.layout.activity_vcffiles_list);
76 | context = this;
77 | if (savedInstanceState != null) {
78 | filePath = savedInstanceState.getString("filePath");
79 | }
80 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
81 | rlNoContacts = (RelativeLayout) findViewById(R.id.rl_no_contacts_in_list);
82 | ActionBar actionBar = getSupportActionBar();
83 | actionBar.setLogo(R.mipmap.ic_launcher);
84 | actionBar.setDisplayUseLogoEnabled(true);
85 | actionBar.setDisplayShowHomeEnabled(true);
86 | askPermissionAndFindFiles(Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_CODE_PERMISSION_READ_EXT_STORAGE, getString(R.string.you_need_to_allow_access_to_ext_storage));
87 | }
88 |
89 | @Override
90 | protected void onSaveInstanceState(Bundle outState) {
91 | outState.putString("filePath", filePath);
92 | }
93 |
94 |
95 | @Override
96 | protected void onStart() {
97 | super.onStart();
98 | }
99 |
100 |
101 |
102 | @Override
103 | protected void onResume() {
104 | super.onResume();
105 | }
106 |
107 | private void askPermissionAndFindFiles(final String permissionType, final int permissionCode, String message) {
108 | int hasExternalStoragePermission = ContextCompat.checkSelfPermission(context, permissionType);
109 | if (hasExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
110 | if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permissionType)) {
111 | showMessageOkCancel(message, new DialogInterface.OnClickListener() {
112 | @Override
113 | public void onClick(DialogInterface dialog, int which) {
114 | ActivityCompat.requestPermissions((Activity) context, new String[]{permissionType}, permissionCode);
115 | }
116 | });
117 | return;
118 | }
119 | ActivityCompat.requestPermissions((Activity) context, new String[]{permissionType}, permissionCode);
120 | return;
121 | }
122 | if (permissionCode == REQUEST_CODE_PERMISSION_READ_EXT_STORAGE)
123 | getSupportLoaderManager().initLoader(LOADER_ID, null, this);
124 | else
125 | //for writing contacts
126 | getAccountsAndShowDialog();
127 | }
128 |
129 |
130 | private void showMessageOkCancel(String message, DialogInterface.OnClickListener listener) {
131 | new AlertDialog.Builder(context)
132 | .setMessage(message)
133 | .setPositiveButton("Ok", listener)
134 | .setNegativeButton("Cancel", null)
135 | .create()
136 | .show();
137 | }
138 |
139 | @Override
140 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
141 | switch (requestCode) {
142 | case REQUEST_CODE_PERMISSION_READ_EXT_STORAGE:
143 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
144 | getSupportLoaderManager().initLoader(LOADER_ID, null, this);
145 | } else {
146 | Toast.makeText(context, "Permission denied", Toast.LENGTH_SHORT).show();
147 | }
148 | break;
149 | case REQUEST_CODE_PERMISSION_WRITE_CONTACTS:
150 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
151 |
152 | } else {
153 | Toast.makeText(context, "Permission denied", Toast.LENGTH_SHORT).show();
154 | }
155 | break;
156 |
157 | default:
158 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
159 | }
160 | }
161 |
162 |
163 | private void getAccountsAndShowDialog() {
164 | AccountManager am = AccountManager.get(this);
165 |
166 | //Since write contacts permission is already asked and GET_ACCOUNTS permission also lies
167 | // in same permission group therefore there is no need to ask for it. It will be allowed automatically.
168 | if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
169 | // TODO: Consider calling
170 | // ActivityCompat#requestPermissions
171 | // here to request the missing permissions, and then overriding
172 | // public void onRequestPermissionsResult(int requestCode, String[] permissions,
173 | // int[] grantResults)
174 | // to handle the case where the user grants the permission. See the documentation
175 | // for ActivityCompat#requestPermissions for more details.
176 | return;
177 | }
178 | Account[] accounts = am.getAccounts();
179 | String account_name = null;
180 | String account_type = null;
181 | for (Account ac : accounts) {
182 | String acname = ac.name;
183 | String actype = ac.type;
184 | System.out.println("Accounts : " + acname + ", " + actype);
185 | //get the google account
186 | if (actype.equals("com.google")) {
187 | account_name = acname;
188 | account_type = actype;
189 | }
190 | }
191 | FragmentManager fragmentm = getSupportFragmentManager();
192 | MyAlertDialog dialog = MyAlertDialog.newInstance(account_name, account_type);
193 | dialog.show(fragmentm, "fragment_select");
194 | }
195 |
196 |
197 | @Override
198 | public Loader onCreateLoader(int id, Bundle args) {
199 | allFilesList = new ArrayList<>();
200 | if (id == LOADER_ID) {
201 | Uri URI = MediaStore.Files.getContentUri("external");
202 | //get all files from external storage(both phones memory and SD card's memory) that ends with .vcf extension
203 | String selection = MediaStore.Files.FileColumns.DATA + " like '%.vcf' or " + MediaStore.Files.FileColumns.DATA + " like '&.VCF'";
204 | return new CursorLoader(this, URI, null, selection, null, MediaStore.Images.ImageColumns.DATE_MODIFIED);
205 | }
206 | return null;
207 | }
208 |
209 | @Override
210 | public void onLoadFinished(Loader loader, Cursor cursor) {
211 | // progressDialog.dismiss();
212 | if (cursor != null) {
213 | Log.d("cursorcount", " " + cursor.getCount() + " " + cursor.getColumnCount());
214 | if (cursor.moveToFirst()) {
215 | do {
216 | String file_name = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME));
217 | int file_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID));
218 | String file_fullpath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
219 | if (file_name == null)
220 | file_name = file_fullpath.substring(file_fullpath.lastIndexOf('/') + 1);
221 | VCFFiles vcfFiles = new VCFFiles(file_id, file_name, file_fullpath);
222 | allFilesList.add(vcfFiles);
223 | System.out.println("namessss: " + file_name + " " + file_id + " " + file_fullpath);
224 | } while (cursor.moveToNext());
225 |
226 | }
227 | if (!allFilesList.isEmpty()) {
228 | recyclerView.setVisibility(View.VISIBLE);
229 | rlNoContacts.setVisibility(View.GONE);
230 | adapter = new VCFFilesAdapter(context);
231 | Log.d("vcffilelistSize", " " + allFilesList.size());
232 | RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
233 | recyclerView.setLayoutManager(mLayoutManager);
234 | recyclerView.setAdapter(adapter);
235 | }
236 | }
237 | }
238 |
239 | @Override
240 | public void onLoaderReset(Loader loader) {
241 | }
242 |
243 |
244 | public void readVCFAndWrite(Context context, String path, String acc_name, String acc_type) {
245 |
246 |
247 | //Log.d("insertToPhoneclick", "true "+path+" "+acc_name+" "+acc_type);
248 | File vcardFile = new File(path);
249 | if (!vcardFile.exists()) {
250 | throw new RuntimeException("vCard file does not exist: " + path);
251 | }
252 | VCardReader reader = null;
253 | try {
254 | reader = new VCardReader(vcardFile);
255 | reader.registerScribe(new AndroidCustomFieldScribe());
256 | ContactOperations operations = new ContactOperations(getApplicationContext(), acc_name, acc_type);
257 | VCard vcard = null;
258 | while ((vcard = reader.readNext()) != null) {
259 | //inserts contacts automatically
260 | operations.insertContact(vcard);
261 | }
262 | } catch (Exception e) {
263 | e.printStackTrace();
264 | } finally {
265 | closeQuietly(reader);
266 | }
267 | }
268 |
269 |
270 | public void readVCFAndWriteToSimCard(Context context, String path, String acc_name, String acc_type) {
271 |
272 | Log.d("insertTosimclick", "true "+path);
273 | //1).inserting into sim card using the basic approach.
274 |
275 | //2).You can also use same method which is used while inserting into phone or gmail account if and only if there exists an SIM card
276 | // accounts in while getting all acounts like we have done for getting gmail account
277 | File vcardFile = new File(path);
278 | Uri simUri = Uri.parse("content://icc/adn");
279 | ContentValues cv = new ContentValues();
280 | if (!vcardFile.exists()) {
281 | throw new RuntimeException("vCard file does not exist: " + path);
282 | }
283 | VCardReader reader = null;
284 | try {
285 | reader = new VCardReader(vcardFile);
286 | reader.registerScribe(new AndroidCustomFieldScribe());
287 | VCard vcard = null;
288 | while ((vcard = reader.readNext()) != null) {
289 | //I'm inserting only one contact here
290 | try {
291 | if (vcard.getFormattedName().getValue() != null && !vcard.getTelephoneNumbers().isEmpty()) {
292 | cv = new ContentValues();
293 | cv.put("tag", vcard.getFormattedName().getValue());
294 | cv.put("number", vcard.getTelephoneNumbers().get(0).getText());
295 | getContentResolver().insert(simUri, cv);
296 | }
297 | }catch(Exception e) {
298 | e.printStackTrace();
299 | }
300 | }
301 | } catch (Exception e) {
302 | e.printStackTrace();
303 | } finally {
304 | closeQuietly(reader);
305 | }
306 | }
307 |
308 | @Override
309 | protected void onStop() {
310 | super.onStop();
311 | try {
312 | getSupportLoaderManager().destroyLoader(LOADER_ID);
313 | } catch (Exception e) {
314 | e.printStackTrace();
315 | }
316 | }
317 |
318 | @Override
319 | public void onAccountSelected(String account_name, String account_type) {
320 | SaveContacts saveContacts;
321 | if (account_name.equals("SIMCARD")) {
322 | saveContacts = new SaveContacts();
323 | saveContacts.execute(null, null, true);
324 | } else if (account_name.equals("Phone")) {
325 | saveContacts = new SaveContacts();
326 | //account_name and account_type are null for phone
327 | saveContacts.execute(null, null, false);
328 | } else {
329 | saveContacts = new SaveContacts();
330 | //google account_name and account_type fetched before
331 | saveContacts.execute(account_name, account_type, false);
332 | }
333 | }
334 |
335 |
336 | ProgressDialog progressDialog;
337 |
338 |
339 | private class SaveContacts extends AsyncTask