From 7cac473c74e93e794e73156cddeb9af51a69a142 Mon Sep 17 00:00:00 2001 From: Tal Hacohen Date: Thu, 30 Mar 2017 12:03:37 +0300 Subject: [PATCH] Import - Split ImportActivity to fragments and activity --- .../ui/importlocal/ImportActivity.java | 281 ++++++------------ .../LocalCalendarImportFragment.java | 266 +++++++++++++++++ .../ui/importlocal/OnImportCallback.java | 11 + .../ui/importlocal/SelectImportMethod.java | 11 + app/src/main/res/layout/activity_import.xml | 59 +--- app/src/main/res/layout/fragment_import.xml | 18 ++ .../main/res/layout/fragment_local_import.xml | 9 + 7 files changed, 415 insertions(+), 240 deletions(-) create mode 100644 app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java create mode 100644 app/src/main/java/com/etesync/syncadapter/ui/importlocal/OnImportCallback.java create mode 100644 app/src/main/java/com/etesync/syncadapter/ui/importlocal/SelectImportMethod.java create mode 100644 app/src/main/res/layout/fragment_import.xml create mode 100644 app/src/main/res/layout/fragment_local_import.xml diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java index 2606b7d5..22049358 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java @@ -5,34 +5,21 @@ import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; -import android.graphics.Typeface; -import android.os.AsyncTask; import android.os.Bundle; -import android.provider.CalendarContract; +import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.BaseExpandableListAdapter; -import android.widget.ExpandableListView; -import android.widget.TextView; -import android.widget.Toast; import com.etesync.syncadapter.App; import com.etesync.syncadapter.R; import com.etesync.syncadapter.model.CollectionInfo; -import com.etesync.syncadapter.resource.CalendarAccount; -import com.etesync.syncadapter.resource.LocalCalendar; -import com.etesync.syncadapter.resource.LocalEvent; import com.etesync.syncadapter.ui.ImportFragment; -import java.util.List; - -import at.bitfire.ical4android.CalendarStorageException; -import at.bitfire.ical4android.Event; - -public class ImportActivity extends AppCompatActivity { +public class ImportActivity extends AppCompatActivity implements SelectImportMethod, OnImportCallback { public final static String EXTRA_ACCOUNT = "account", EXTRA_COLLECTION_INFO = "collectionInfo"; @@ -57,56 +44,63 @@ public class ImportActivity extends AppCompatActivity { account = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT); info = (CollectionInfo) getIntent().getExtras().getSerializable(EXTRA_COLLECTION_INFO); - findViewById(R.id.import_button_account).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View aView) { - importAccount(); - } - }); - - findViewById(R.id.import_button_file).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View aView) { - importFile(); - } - }); + if (savedInstanceState == null) + getSupportFragmentManager().beginTransaction() + .add(R.id.fragment_container, new ImportActivity.SelectImportFragment()) + .commit(); } - private void importFile() { + @Override + public void importFile() { getSupportFragmentManager().beginTransaction() .add(ImportFragment.newInstance(account, info), null) .commit(); } - private void importAccount() { - final List calendarAccountList = CalendarAccount.loadAll(getContentResolver()); + @Override + public void importAccount() { + if (info.type == CollectionInfo.Type.CALENDAR) { + getSupportFragmentManager().beginTransaction() + .replace(R.id.fragment_container, + LocalCalendarImportFragment.newInstance(account, info)) + .addToBackStack(LocalCalendarImportFragment.class.getName()) + .commit(); + } else if (info.type == CollectionInfo.Type.ADDRESS_BOOK) { + getSupportFragmentManager().beginTransaction() + .replace(R.id.fragment_container, + LocalContactImportFragment.newInstance(account, info)) + .addToBackStack(LocalContactImportFragment.class.getName()) + .commit(); + } + } - ExpandableListView listCalendar = (ExpandableListView) findViewById(R.id.calendars); - - final ExpandableListAdapter adapter = new ExpandableListAdapter(this, calendarAccountList); - listCalendar.setAdapter(adapter); - - listCalendar.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { - @Override - public boolean onChildClick(ExpandableListView aExpandableListView, View aView, int groupPosition, int childPosition, long aL) { - new ImportEvents().execute(calendarAccountList.get(groupPosition).calendars.get(childPosition)); - return false; - } - }); + private void popBackStack() { + if (!getSupportFragmentManager().popBackStackImmediate()) { + finish(); + } else { + setTitle(getString(R.string.import_dialog_title)); + } } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { - if (!getSupportFragmentManager().popBackStackImmediate()) { - finish(); - } + popBackStack(); return true; } return false; } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + popBackStack(); + return true; + } + return super.onKeyDown(keyCode, event); + } + @Override protected void onResume() { super.onResume(); @@ -125,152 +119,65 @@ public class ImportActivity extends AppCompatActivity { app.getCertManager().appInForeground = false; } - public class ExpandableListAdapter extends BaseExpandableListAdapter { - - private Context context; - private List calendarAccounts; - - public ExpandableListAdapter(Context context, List calendarAccounts) { - this.context = context; - this.calendarAccounts = calendarAccounts; - } - - @Override - public Object getChild(int groupPosition, int childPosititon) { - return calendarAccounts.get(groupPosition).calendars - .get(childPosititon).getDisplayName(); - } - - @Override - public long getChildId(int groupPosition, int childPosition) { - return childPosition; - } - - @Override - public View getChildView(int groupPosition, final int childPosition, - boolean isLastChild, View convertView, ViewGroup parent) { - - final String childText = (String) getChild(groupPosition, childPosition); - - if (convertView == null) { - LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = inflater.inflate(R.layout.list_item, null); - } - //Todo add viewholder after we decide about the UI - - TextView txtListChild = (TextView) convertView - .findViewById(R.id.lblListItem); - - txtListChild.setText(childText); - return convertView; - } - - @Override - public int getChildrenCount(int groupPosition) { - return calendarAccounts.get(groupPosition).calendars - .size(); - } - - @Override - public Object getGroup(int groupPosition) { - return calendarAccounts.get(groupPosition).toString(); - } - - @Override - public int getGroupCount() { - return calendarAccounts.size(); - } - - @Override - public long getGroupId(int groupPosition) { - return groupPosition; - } - - @Override - public View getGroupView(int groupPosition, boolean isExpanded, - View convertView, ViewGroup parent) { - String headerTitle = (String) getGroup(groupPosition); - if (convertView == null) { - LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = inflater.inflate(R.layout.list_group, null); - } - //Todo add viewholder after we decide about the UI - - TextView lblListHeader = (TextView) convertView - .findViewById(R.id.lblListHeader); - lblListHeader.setTypeface(null, Typeface.BOLD); - lblListHeader.setText(headerTitle); - - return convertView; - } - - @Override - public boolean hasStableIds() { - return false; - } - - @Override - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } + @Override + public void onImportSuccess() { + //todo tom what would you like to do? + finish(); } - protected class ImportEvents extends AsyncTask { - ProgressDialog progressDialog; + @Override + public void onImportFailed() { + //todo tom what would you like to do? + finish(); + } + + public static class SelectImportFragment extends Fragment { + + private SelectImportMethod mSelectImportMethod; @Override - protected void onPreExecute() { - progressDialog = new ProgressDialog(ImportActivity.this); - progressDialog.setTitle(R.string.import_dialog_title); - progressDialog.setMessage(getString(R.string.import_dialog_adding_entries)); - progressDialog.setCanceledOnTouchOutside(false); - progressDialog.setIndeterminate(false); - progressDialog.setIcon(R.drawable.ic_import_export_black); - progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - progressDialog.show(); - } - - @Override - protected Boolean doInBackground(LocalCalendar... calendars) { - return importEvents(calendars[0]); - } - - @Override - protected void onProgressUpdate(Integer... progress) { - if (progressDialog != null) - progressDialog.setProgress(progress[0]); - } - - @Override - protected void onPostExecute(Boolean result) { - progressDialog.dismiss(); - } - - private boolean importEvents(LocalCalendar fromCalendar) { + public void onAttach(Context context) { + super.onAttach(context); + // This makes sure that the container activity has implemented + // the callback interface. If not, it throws an exception try { - LocalCalendar localCalendar = LocalCalendar.findByName(account, - ImportActivity.this.getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), - LocalCalendar.Factory.INSTANCE, info.url); - LocalEvent[] localEvents = fromCalendar.getAll(); - progressDialog.setMax(localEvents.length); - int progress = 0; - for (LocalEvent currentLocalEvent : localEvents) { - Event event = currentLocalEvent.getEvent(); - try { - LocalEvent localEvent = new LocalEvent(localCalendar, event, event.uid, null); - localEvent.addAsDirty(); - } catch (CalendarStorageException e) { - e.printStackTrace(); - } - publishProgress(++progress); - } - return true; - } catch (Exception aE) { - aE.printStackTrace(); - return false; + mSelectImportMethod = (SelectImportMethod) getActivity(); + } catch (ClassCastException e) { + throw new ClassCastException(getActivity().toString() + + " must implement MyInterface "); } } + + @SuppressWarnings("deprecation") + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + // This makes sure that the container activity has implemented + // the callback interface. If not, it throws an exception + try { + mSelectImportMethod = (SelectImportMethod) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement MyInterface "); + } + } + + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_import, container, false); + v.findViewById(R.id.import_button_account).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View aView) { + mSelectImportMethod.importAccount(); + } + }); + + v.findViewById(R.id.import_button_file).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View aView) { + mSelectImportMethod.importFile(); + } + }); + return v; + } } } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java new file mode 100644 index 00000000..6d2ebf17 --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/LocalCalendarImportFragment.java @@ -0,0 +1,266 @@ +package com.etesync.syncadapter.ui.importlocal; + +import android.accounts.Account; +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Context; +import android.graphics.Typeface; +import android.os.AsyncTask; +import android.os.Bundle; +import android.provider.CalendarContract; +import android.support.v4.app.ListFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseExpandableListAdapter; +import android.widget.ExpandableListView; +import android.widget.TextView; + +import com.etesync.syncadapter.R; +import com.etesync.syncadapter.model.CollectionInfo; +import com.etesync.syncadapter.resource.CalendarAccount; +import com.etesync.syncadapter.resource.LocalCalendar; +import com.etesync.syncadapter.resource.LocalEvent; + +import java.util.List; + +import at.bitfire.ical4android.CalendarStorageException; +import at.bitfire.ical4android.Event; + +import static com.etesync.syncadapter.Constants.KEY_ACCOUNT; +import static com.etesync.syncadapter.Constants.KEY_COLLECTION_INFO; + +public class LocalCalendarImportFragment extends ListFragment { + + private Account account; + private CollectionInfo info; + private OnImportCallback importCallback; + + public static LocalCalendarImportFragment newInstance(Account account, CollectionInfo info) { + LocalCalendarImportFragment frag = new LocalCalendarImportFragment(); + Bundle args = new Bundle(1); + args.putParcelable(KEY_ACCOUNT, account); + args.putSerializable(KEY_COLLECTION_INFO, info); + frag.setArguments(args); + return frag; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setRetainInstance(true); + + account = getArguments().getParcelable(KEY_ACCOUNT); + info = (CollectionInfo) getArguments().getSerializable(KEY_COLLECTION_INFO); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_local_import, container, false); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + importAccount(); + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + // This makes sure that the container activity has implemented + // the callback interface. If not, it throws an exception + try { + importCallback = (OnImportCallback) getActivity(); + } catch (ClassCastException e) { + throw new ClassCastException(getActivity().toString() + + " must implement MyInterface "); + } + } + + @SuppressWarnings("deprecation") + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + // This makes sure that the container activity has implemented + // the callback interface. If not, it throws an exception + try { + importCallback = (OnImportCallback) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement MyInterface "); + } + } + + protected void importAccount() { + final List calendarAccountList = CalendarAccount.loadAll(getContext().getContentResolver()); + + ExpandableListView listCalendar = (ExpandableListView) getListView(); + + final LocalCalendarImportFragment.ExpandableListAdapter adapter = + new LocalCalendarImportFragment.ExpandableListAdapter(getContext(), calendarAccountList); + listCalendar.setAdapter(adapter); + + listCalendar.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { + @Override + public boolean onChildClick(ExpandableListView aExpandableListView, View aView, int groupPosition, int childPosition, long aL) { + new ImportEvents().execute(calendarAccountList.get(groupPosition).calendars.get(childPosition)); + return false; + } + }); + } + + + private class ExpandableListAdapter extends BaseExpandableListAdapter { + + private Context context; + private List calendarAccounts; + + public ExpandableListAdapter(Context context, List calendarAccounts) { + this.context = context; + this.calendarAccounts = calendarAccounts; + } + + @Override + public Object getChild(int groupPosition, int childPosititon) { + return calendarAccounts.get(groupPosition).calendars + .get(childPosititon).getDisplayName(); + } + + @Override + public long getChildId(int groupPosition, int childPosition) { + return childPosition; + } + + @Override + public View getChildView(int groupPosition, final int childPosition, + boolean isLastChild, View convertView, ViewGroup parent) { + + final String childText = (String) getChild(groupPosition, childPosition); + + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.list_item, null); + } + //Todo add viewholder after we decide about the UI + + TextView txtListChild = (TextView) convertView + .findViewById(R.id.lblListItem); + + txtListChild.setText(childText); + return convertView; + } + + @Override + public int getChildrenCount(int groupPosition) { + return calendarAccounts.get(groupPosition).calendars + .size(); + } + + @Override + public Object getGroup(int groupPosition) { + return calendarAccounts.get(groupPosition).toString(); + } + + @Override + public int getGroupCount() { + return calendarAccounts.size(); + } + + @Override + public long getGroupId(int groupPosition) { + return groupPosition; + } + + @Override + public View getGroupView(int groupPosition, boolean isExpanded, + View convertView, ViewGroup parent) { + String headerTitle = (String) getGroup(groupPosition); + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.list_group, null); + } + //Todo add viewholder after we decide about the UI + + TextView lblListHeader = (TextView) convertView + .findViewById(R.id.lblListHeader); + lblListHeader.setTypeface(null, Typeface.BOLD); + lblListHeader.setText(headerTitle); + + return convertView; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } + } + + protected class ImportEvents extends AsyncTask { + ProgressDialog progressDialog; + + @Override + protected void onPreExecute() { + progressDialog = new ProgressDialog(getActivity()); + progressDialog.setTitle(R.string.import_dialog_title); + progressDialog.setMessage(getString(R.string.import_dialog_adding_entries)); + progressDialog.setCanceledOnTouchOutside(false); + progressDialog.setCancelable(false); + progressDialog.setIndeterminate(false); + progressDialog.setIcon(R.drawable.ic_import_export_black); + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progressDialog.show(); + } + + @Override + protected Boolean doInBackground(LocalCalendar... calendars) { + return importEvents(calendars[0]); + } + + @Override + protected void onProgressUpdate(Integer... progress) { + if (progressDialog != null) + progressDialog.setProgress(progress[0]); + } + + @Override + protected void onPostExecute(Boolean result) { + progressDialog.dismiss(); + if (result) importCallback.onImportSuccess(); + else importCallback.onImportFailed(); + } + + private boolean importEvents(LocalCalendar fromCalendar) { + try { + LocalCalendar localCalendar = LocalCalendar.findByName(account, + getContext().getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), + LocalCalendar.Factory.INSTANCE, info.url); + LocalEvent[] localEvents = fromCalendar.getAll(); + progressDialog.setMax(localEvents.length); + int progress = 0; + for (LocalEvent currentLocalEvent : localEvents) { + Event event = currentLocalEvent.getEvent(); + try { + LocalEvent localEvent = new LocalEvent(localCalendar, event, event.uid, null); + localEvent.addAsDirty(); + } catch (CalendarStorageException e) { + e.printStackTrace(); + } + publishProgress(++progress); + } + return true; + } catch (Exception aE) { + aE.printStackTrace(); + return false; + } + } + } +} diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/OnImportCallback.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/OnImportCallback.java new file mode 100644 index 00000000..47e2b8ab --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/OnImportCallback.java @@ -0,0 +1,11 @@ +package com.etesync.syncadapter.ui.importlocal; + +/** + * Created by tal on 30/03/17. + */ + +public interface OnImportCallback { + void onImportSuccess(); + + void onImportFailed(); +} diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/SelectImportMethod.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/SelectImportMethod.java new file mode 100644 index 00000000..01322a56 --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/SelectImportMethod.java @@ -0,0 +1,11 @@ +package com.etesync.syncadapter.ui.importlocal; + +/** + * Created by tal on 30/03/17. + */ + +public interface SelectImportMethod { + void importFile(); + + void importAccount(); +} diff --git a/app/src/main/res/layout/activity_import.xml b/app/src/main/res/layout/activity_import.xml index a9fad99b..008f1127 100644 --- a/app/src/main/res/layout/activity_import.xml +++ b/app/src/main/res/layout/activity_import.xml @@ -1,54 +1,7 @@ - - -