Skip to content

Commit

Permalink
Fixed RecoverHelper, backup works from Downloads, external and intern…
Browse files Browse the repository at this point in the history
…al storage.

Made list view elements 2 lines.
  • Loading branch information
Djonique committed Nov 4, 2017
1 parent 86c7da2 commit 297fc53
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void onRequestPermissionsResult(int requestCode,
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_MANAGER && resultCode == RESULT_OK) {
new RecoverHelper(this).recoverRecords(data.getData().getPath());
new RecoverHelper(this).recoverRecords(this, data.getData());
}
}

Expand Down Expand Up @@ -405,7 +405,7 @@ public boolean onPreferenceClick(Preference preference) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_MANAGER && resultCode == RESULT_OK) {
new RecoverHelper(getActivity()).recoverRecords(data.getData().getPath());
new RecoverHelper(getActivity()).recoverRecords(getActivity(), data.getData());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -217,14 +218,14 @@ private void changeCardViewBackgroundColor(long date, CardView cardView, TextVie
boolean today = daysLeft.equals(context.getString(R.string.today));
if (Utils.isBirthdayPassed(date)) {
textView.setVisibility(View.GONE);
cardView.setCardBackgroundColor(context.getResources().getColor(R.color.cardview_background));
cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.cardview_background));
} else {
textView.setVisibility(View.VISIBLE);
if (today) {
cardView.setCardBackgroundColor(context.getResources().getColor(R.color.cardview_birthday));
cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.cardview_birthday));
textView.setText(context.getString(R.string.today));
} else {
cardView.setCardBackgroundColor(context.getResources().getColor(R.color.cardview_background));
cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.cardview_background));
String summary = context.getString(R.string.days_left) + ": " + daysLeft;
textView.setText(summary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

public class ExportHelper {

// XML constants
private static final String RECORDS = "records";
private static final String PERSON = "person";
private static final String NAME = "name";
Expand All @@ -54,6 +55,7 @@ public class ExportHelper {
private static final String BACKUP = "backup";
private static final String UTF_8 = "UTF-8";

// Exceptions constants
private static final String ILLEGAL_ARGUMENT_EXCEPTION = "IllegalArgumentException";
private static final String ILLEGAL_STATE_EXCEPTION = "IllegalStateException";
private static final String IO_EXCEPTION = "IOException";
Expand Down
60 changes: 59 additions & 1 deletion app/src/main/java/com/djonique/birdays/backup/RecoverHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package com.djonique.birdays.backup;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.widget.Toast;

import com.djonique.birdays.R;
Expand All @@ -38,24 +43,35 @@

public class RecoverHelper {

// XML constants
private static final String PERSON = "person";
private static final String NAME = "name";
private static final String DATE = "date";
private static final String YEAR_UNKNOWN = "year_unknown";
private static final String PHONE_NUMBER = "phone_number";
private static final String EMAIL = "email";

// Exceptions constants
private static final String XML_PULL_PARSER_EXCEPTION = "XmlPullParserException";
private static final String FILE_NOT_FOUND_EXCEPTION = "FileNotFoundException";
private static final String IO_EXCEPTION = "IOException";

// Path constants
private static final String PRIMARY = "primary";
private static final String CONTENT_DOWNLOADS = "content://downloads/public_downloads";
private static final String AUTHORITY_EXTERNAL_STORAGE = "com.android.externalstorage.documents";
private static final String AUTHORITY_DOWNLOADS = "com.android.providers.downloads.documents";
private static final String COLUMN_DATA = "_data";

private Context context;

public RecoverHelper(Context context) {
this.context = context;
}

public void recoverRecords(String path) {
public void recoverRecords(Context context, Uri uri) {
String path = getPath(context, uri);
if (path == null) path = uri.getPath();
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
Expand Down Expand Up @@ -129,4 +145,46 @@ private void parseXml(XmlPullParser parser) {
Toast.makeText(context, IO_EXCEPTION, Toast.LENGTH_LONG).show();
}
}

private String getPath(Context context, Uri uri) {
if (isExternalStorageDocument(uri)) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];

if (PRIMARY.equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse(CONTENT_DOWNLOADS), Long.valueOf(id));
return getDataColumn(context, contentUri);
}
return null;
}

private boolean isExternalStorageDocument(Uri uri) {
return uri.getAuthority().equals(AUTHORITY_EXTERNAL_STORAGE);
}

private boolean isDownloadsDocument(Uri uri) {
return uri.getAuthority().equals(AUTHORITY_DOWNLOADS);
}

private String getDataColumn(Context context, Uri uri) {
Cursor cursor = null;
String column = COLUMN_DATA;
String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void onAttach(Activity activity) {
public Dialog onCreateDialog(Bundle savedInstanceState) {

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.add_new_record);
builder.setTitle(R.string.new_record);

calendar = Calendar.getInstance();
final Person person = new Person();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<TextView
style="@style/EditActivity.Secondary.TextView"
android:layout_below="@+id/textview_detail_date"
android:text="@string/date" />
android:text="@string/birthday" />
</RelativeLayout>

<!-- DaysLeft -->
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/description_famous_list_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<TextView
android:id="@+id/textview_famous_name"
style="@style/Primary.TextView"
android:maxLines="1" />
android:maxLines="2" />

<TextView
android:id="@+id/textview_famous_date"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/description_list_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<TextView
android:id="@+id/textview_all_name"
style="@style/Material.Primary.TextView"
android:maxLines="1" />
android:maxLines="2" />

<TextView
android:id="@+id/textview_all_date"
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values-be/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Удалить</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">Новая запись</string>
<string name="new_record">Новая запись</string>
<string name="add_from_contacts">Добавить из контактов</string>
<string name="open_contacts_error">Это действие выполнить невозможно из-за особенностей устройства</string>
<string name="name">Имя</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Запись добавлена</string>

<!-- DetailActivity -->
<string name="date">День рождения</string>
<string name="birthday">День рождения</string>
<string name="days_left">Дней осталось</string>
<string name="today">Сегодня</string>
<string name="days_since_birth">Дней с момента рождения</string>
Expand Down Expand Up @@ -145,7 +145,7 @@
<string name="exporting_records">Экспортирование…</string>
<string name="backup_finished">Резервное копирование завершено, XML-файл находится в\u0020</string>
<string name="recover_records">Восстановить записи (Beta)</string>
<string name="recover_records_summary">Выберите XML-файл с резервной копией во внешнем хранилище</string>
<string name="recover_records_summary">Выберите XML-файл с резервной копией</string>
<string name="recover_records_error">Невозможно выполнить действие, пожалуйста, установите диспетчер файлов</string>
<string name="records_recovered">Записи восстановлены</string>
<!-- General -->
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Löschen</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">Neuer Eintrag</string>
<string name="new_record">Neuer Eintrag</string>
<string name="add_from_contacts">Aus Kontakten hinzufügen</string>
<string name="open_contacts_error">Aktion kann wegen Geräteeigenschaften nicht ausgeführt werden</string>
<string name="name">Name</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Eintrag hinzugefügt</string>

<!-- DetailActivity -->
<string name="date">Geburtstag</string>
<string name="birthday">Geburtstag</string>
<string name="days_left">Tage bis zum nächsten Geburtstag</string>
<string name="today">Heute</string>
<string name="days_since_birth">Tage seit der Geburt</string>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Delete</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">New record</string>
<string name="new_record">New record</string>
<string name="add_from_contacts">Add from contacts</string>
<string name="open_contacts_error">Unable to perform action due to device features</string>
<string name="name">Name</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Record added</string>

<!-- DetailActivity -->
<string name="date">Birthday</string>
<string name="birthday">Birthday</string>
<string name="days_left">Days until next birthday</string>
<string name="today">Today</string>
<string name="days_since_birth">Days since birth</string>
Expand Down Expand Up @@ -145,7 +145,7 @@
<string name="exporting_records">Exporting records…</string>
<string name="backup_finished">Backup finished, XML file in the\u0020</string>
<string name="recover_records">Recover records (Beta)</string>
<string name="recover_records_summary">Choose the XML file with backup in external storage</string>
<string name="recover_records_summary">Choose the XML file with backup</string>
<string name="recover_records_error">Unable to perform action, please install file manager</string>
<string name="records_recovered">Records recovered</string>
<!-- General -->
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Eliminar</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">Nueva entrada</string>
<string name="new_record">Nueva entrada</string>
<string name="add_from_contacts">Añadir desde contactos</string>
<string name="open_contacts_error">Es imposible realizar esta acción en este dispositivo</string>
<string name="name">Nombre</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Entrada añadida</string>

<!-- DetailActivity -->
<string name="date">Cumpleaños</string>
<string name="birthday">Cumpleaños</string>
<string name="days_left">Días hasta el próximo cumpleaños</string>
<string name="today">Hoy</string>
<string name="days_since_birth">Días desde su nacimiento</string>
Expand Down Expand Up @@ -145,7 +145,7 @@
<string name="exporting_records">Exporting records…</string>
<string name="backup_finished">Backup finished, XML file in the\u0020</string>
<string name="recover_records">Recover records (Beta)</string>
<string name="recover_records_summary">Choose the XML file with backup in external storage</string>
<string name="recover_records_summary">Choose the XML file with backup</string>
<string name="recover_records_error">Unable to perform action, please install file manager</string>
<string name="records_recovered">Records recovered</string>
<!-- General -->
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values-kk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Удалить</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">Новая запись</string>
<string name="new_record">Новая запись</string>
<string name="add_from_contacts">Добавить из контактов</string>
<string name="open_contacts_error">Это действие выполнить невозможно из-за особенностей устройства</string>
<string name="name">Имя</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Запись добавлена</string>

<!-- DetailActivity -->
<string name="date">День рождения</string>
<string name="birthday">День рождения</string>
<string name="days_left">Дней осталось</string>
<string name="today">Сегодня</string>
<string name="days_since_birth">Дней с момента рождения</string>
Expand Down Expand Up @@ -145,7 +145,7 @@
<string name="exporting_records">Экспортирование…</string>
<string name="backup_finished">Резервное копирование завершено, XML-файл находится в\u0020</string>
<string name="recover_records">Восстановить записи (Beta)</string>
<string name="recover_records_summary">Выберите XML-файл с резервной копией во внешнем хранилище</string>
<string name="recover_records_summary">Выберите XML-файл с резервной копией</string>
<string name="recover_records_error">Невозможно выполнить действие, пожалуйста, установите диспетчер файлов</string>
<string name="records_recovered">Записи восстановлены</string>
<!-- General -->
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Удалить</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">Новая запись</string>
<string name="new_record">Новая запись</string>
<string name="add_from_contacts">Добавить из контактов</string>
<string name="open_contacts_error">Это действие выполнить невозможно из-за особенностей устройства</string>
<string name="name">Имя</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Запись добавлена</string>

<!-- DetailActivity -->
<string name="date">День рождения</string>
<string name="birthday">День рождения</string>
<string name="days_left">Дней осталось</string>
<string name="today">Сегодня</string>
<string name="days_since_birth">Дней с момента рождения</string>
Expand Down Expand Up @@ -145,7 +145,7 @@
<string name="exporting_records">Экспортирование…</string>
<string name="backup_finished">Резервное копирование завершено, XML-файл находится в\u0020</string>
<string name="recover_records">Восстановить записи (Beta)</string>
<string name="recover_records_summary">Выберите XML-файл с резервной копией во внешнем хранилище</string>
<string name="recover_records_summary">Выберите XML-файл с резервной копией</string>
<string name="recover_records_error">Невозможно выполнить действие, пожалуйста, установите диспетчер файлов</string>
<string name="records_recovered">Записи восстановлены</string>
<!-- General -->
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-sw600dp/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<resources>
<!-- default dimens -->
<dimen name="default_padding">24sp</dimen>
<dimen name="default_layout_height">96sp</dimen>
<dimen name="default_left_margin_56sp">72sp</dimen>
<dimen name="default_left_margin_72sp">96sp</dimen>

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="action_delete">Видалити</string>

<!-- NewPersonDialogFragment -->
<string name="add_new_record">Новий запис</string>
<string name="new_record">Новий запис</string>
<string name="add_from_contacts">Додати з контактів</string>
<string name="open_contacts_error">Цю дію неможливо виконати через особливості пристрою</string>
<string name="name">Ім\'я</string>
Expand All @@ -68,7 +68,7 @@
<string name="record_added">Запис доданий</string>

<!-- DetailActivity -->
<string name="date">День народження</string>
<string name="birthday">День народження</string>
<string name="days_left">Днів лишилось</string>
<string name="today">Сьогодні</string>
<string name="days_since_birth">Днів із моменту народження</string>
Expand Down Expand Up @@ -145,7 +145,7 @@
<string name="exporting_records">Експортування…</string>
<string name="backup_finished">Резервне копіювання завершене, XML-файл знаходиться в\u0020</string>
<string name="recover_records">Відновити записи (Beta)</string>
<string name="recover_records_summary">Виберіть XML-файл із резервною копією в зовнішньому сховищі</string>
<string name="recover_records_summary">Виберіть XML-файл із резервною копією</string>
<string name="recover_records_error">Неможливо виконати дію, будь ласка, встановіть диспетчер файлів</string>
<string name="records_recovered">Записи відновлені</string>
<!-- General -->
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<resources>
<!-- default dimens -->
<dimen name="default_padding">16sp</dimen>
<dimen name="default_layout_height">72sp</dimen>
<dimen name="default_left_margin_56sp">56sp</dimen>
<dimen name="default_left_margin_72sp">72sp</dimen>

Expand Down
Loading

0 comments on commit 297fc53

Please sign in to comment.