1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Import - Split ImportActivity to fragments and activity

This commit is contained in:
Tal Hacohen 2017-03-30 12:03:37 +03:00
parent 33a310344b
commit 7cac473c74
7 changed files with 415 additions and 240 deletions

View File

@ -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<CalendarAccount> 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<CalendarAccount> calendarAccounts;
public ExpandableListAdapter(Context context, List<CalendarAccount> 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<LocalCalendar, Integer, Boolean> {
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;
}
}
}

View File

@ -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<CalendarAccount> 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<CalendarAccount> calendarAccounts;
public ExpandableListAdapter(Context context, List<CalendarAccount> 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<LocalCalendar, Integer, Boolean> {
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;
}
}
}
}

View File

@ -0,0 +1,11 @@
package com.etesync.syncadapter.ui.importlocal;
/**
* Created by tal on 30/03/17.
*/
public interface OnImportCallback {
void onImportSuccess();
void onImportFailed();
}

View File

@ -0,0 +1,11 @@
package com.etesync.syncadapter.ui.importlocal;
/**
* Created by tal on 30/03/17.
*/
public interface SelectImportMethod {
void importFile();
void importAccount();
}

View File

@ -1,54 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_margin"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/import_button_file"
android:text="@string/import_button_file"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/import_button_account"
android:text="@string/import_button_account"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/caldav_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/toolbar_theme"
style="@style/toolbar_style"
app:navigationIcon="@drawable/ic_event_light"
app:title="@string/settings_caldav"
android:elevation="2dp" tools:ignore="UnusedAttribute"/>
<ProgressBar
android:id="@+id/caldav_refreshing"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:indeterminate="true"/>
<ExpandableListView
android:id="@+id/calendars"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:descendantFocusability="beforeDescendants"/>
</LinearLayout>
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_margin">
</FrameLayout>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/import_button_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/import_button_file"/>
<Button
android:id="@+id/import_button_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/import_button_account"/>
</LinearLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:descendantFocusability="beforeDescendants"/>