mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-11-24 13:56:14 +03:00
Ask to import account on migration
This commit is contained in:
parent
7b1a8987df
commit
fffbd2b15d
4 changed files with 95 additions and 41 deletions
|
@ -200,9 +200,7 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
|
||||||
try { // to get current account from SingleAccountHelper
|
try { // to get current account from SingleAccountHelper
|
||||||
selectAccount(SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()).name);
|
selectAccount(SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()).name);
|
||||||
Log.v(getClass().getSimpleName(), "NextcloudRequest account: " + localAccount);
|
Log.v(getClass().getSimpleName(), "NextcloudRequest account: " + localAccount);
|
||||||
} catch (NextcloudFilesAppAccountNotFoundException e) {
|
} catch (NoCurrentAccountSelectedException | NextcloudFilesAppAccountNotFoundException e) {
|
||||||
e.printStackTrace();
|
|
||||||
} catch (NoCurrentAccountSelectedException e) {
|
|
||||||
if (db.hasAccounts()) { // If nothing is stored in SingleAccountHelper, check db for accounts
|
if (db.hasAccounts()) { // If nothing is stored in SingleAccountHelper, check db for accounts
|
||||||
selectAccount(db.getAccounts().get(0).getAccountName());
|
selectAccount(db.getAccounts().get(0).getAccountName());
|
||||||
} else {
|
} else {
|
||||||
|
@ -740,14 +738,6 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
|
||||||
AccountImporter.onActivityResult(requestCode, resultCode, data, this, (SingleSignOnAccount account) -> {
|
|
||||||
Log.v(getClass().getSimpleName(), "Added account: " + "name:" + account.name + ", " + account.url + ", userId" + account.userId);
|
|
||||||
db.addAccount(account.url, account.userId, account.name);
|
|
||||||
selectAccount(account.name);
|
|
||||||
clickHeader();
|
|
||||||
drawerLayout.closeDrawer(GravityCompat.START);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Check which request we're responding to
|
// Check which request we're responding to
|
||||||
if (requestCode == create_note_cmd) {
|
if (requestCode == create_note_cmd) {
|
||||||
// Make sure the request was successful
|
// Make sure the request was successful
|
||||||
|
@ -771,6 +761,14 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
|
||||||
// Recreate activity completely, because theme switchting makes problems when only invalidating the views.
|
// Recreate activity completely, because theme switchting makes problems when only invalidating the views.
|
||||||
// @see https://github.com/stefan-niedermann/nextcloud-notes/issues/529
|
// @see https://github.com/stefan-niedermann/nextcloud-notes/issues/529
|
||||||
recreate();
|
recreate();
|
||||||
|
} else {
|
||||||
|
AccountImporter.onActivityResult(requestCode, resultCode, data, this, (SingleSignOnAccount account) -> {
|
||||||
|
Log.v(getClass().getSimpleName(), "Added account: " + "name:" + account.name + ", " + account.url + ", userId" + account.userId);
|
||||||
|
db.addAccount(account.url, account.userId, account.name);
|
||||||
|
selectAccount(account.name);
|
||||||
|
clickHeader();
|
||||||
|
drawerLayout.closeDrawer(GravityCompat.START);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
package it.niedermann.owncloud.notes.persistence;
|
package it.niedermann.owncloud.notes.persistence;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountManager;
|
||||||
|
import android.app.Activity;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ShortcutManager;
|
import android.content.pm.ShortcutManager;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
@ -18,7 +23,13 @@ import android.util.Log;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.WorkerThread;
|
import androidx.annotation.WorkerThread;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import com.nextcloud.android.sso.Constants;
|
||||||
|
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
|
||||||
|
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -27,6 +38,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import it.niedermann.owncloud.notes.R;
|
import it.niedermann.owncloud.notes.R;
|
||||||
|
import it.niedermann.owncloud.notes.android.activity.NotesListViewActivity;
|
||||||
import it.niedermann.owncloud.notes.android.appwidget.NoteListWidget;
|
import it.niedermann.owncloud.notes.android.appwidget.NoteListWidget;
|
||||||
import it.niedermann.owncloud.notes.android.appwidget.SingleNoteWidget;
|
import it.niedermann.owncloud.notes.android.appwidget.SingleNoteWidget;
|
||||||
import it.niedermann.owncloud.notes.model.CloudNote;
|
import it.niedermann.owncloud.notes.model.CloudNote;
|
||||||
|
@ -37,6 +49,8 @@ import it.niedermann.owncloud.notes.model.NavigationAdapter;
|
||||||
import it.niedermann.owncloud.notes.util.ICallback;
|
import it.niedermann.owncloud.notes.util.ICallback;
|
||||||
import it.niedermann.owncloud.notes.util.NoteUtil;
|
import it.niedermann.owncloud.notes.util.NoteUtil;
|
||||||
|
|
||||||
|
import static com.nextcloud.android.sso.AccountImporter.CHOOSE_ACCOUNT_SSO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helps to add, get, update and delete Notes with the option to trigger a Resync with the Server.
|
* Helps to add, get, update and delete Notes with the option to trigger a Resync with the Server.
|
||||||
*/
|
*/
|
||||||
|
@ -72,13 +86,13 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper {
|
||||||
|
|
||||||
private NoteSQLiteOpenHelper(Context context) {
|
private NoteSQLiteOpenHelper(Context context) {
|
||||||
super(context, database_name, null, database_version);
|
super(context, database_name, null, database_version);
|
||||||
this.context = context.getApplicationContext();
|
this.context = context;
|
||||||
serverSyncHelper = NoteServerSyncHelper.getInstance(this);
|
serverSyncHelper = NoteServerSyncHelper.getInstance(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NoteSQLiteOpenHelper getInstance(Context context) {
|
public static NoteSQLiteOpenHelper getInstance(Context context) {
|
||||||
if (instance == null)
|
if (instance == null)
|
||||||
return instance = new NoteSQLiteOpenHelper(context.getApplicationContext());
|
return instance = new NoteSQLiteOpenHelper(context);
|
||||||
else
|
else
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
@ -163,10 +177,74 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper {
|
||||||
values.put(key_account_id, 1);
|
values.put(key_account_id, 1);
|
||||||
db.update(table_notes, values, key_account_id + " = ?", new String[]{"NULL"});
|
db.update(table_notes, values, key_account_id + " = ?", new String[]{"NULL"});
|
||||||
|
|
||||||
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
|
|
||||||
editor.remove("notes_last_etag");
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
editor.remove("notes_last_modified");
|
|
||||||
editor.apply();
|
String username = sharedPreferences.getString("settingsUsername", "");
|
||||||
|
String url = sharedPreferences.getString("settingsUrl", "");
|
||||||
|
if (url != null && url.endsWith("/")) {
|
||||||
|
url = url.substring(0, url.length() - 1);
|
||||||
|
try {
|
||||||
|
String accountName = username + "@" + new URL(url).getHost();
|
||||||
|
|
||||||
|
ContentValues migratedAccountValues = new ContentValues();
|
||||||
|
migratedAccountValues.put(key_url, url);
|
||||||
|
migratedAccountValues.put(key_username, username);
|
||||||
|
migratedAccountValues.put(key_account_name, accountName);
|
||||||
|
db.insert(table_accounts, null, migratedAccountValues);
|
||||||
|
|
||||||
|
if (context instanceof NotesListViewActivity) {
|
||||||
|
// Partially copied from AccountImporter
|
||||||
|
Activity activity = (Activity) context;
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||||
|
// Do something for lollipop and above versions
|
||||||
|
Log.d(getClass().getSimpleName(), "Permission not granted!");
|
||||||
|
throw new AndroidGetAccountsPermissionNotGranted();
|
||||||
|
} else {
|
||||||
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
Log.d(getClass().getSimpleName(), "Permission granted!");
|
||||||
|
ArrayList<Account> accs = new ArrayList<>();
|
||||||
|
accs.add(new Account(accountName, Constants.ACCOUNT_TYPE_PROD));
|
||||||
|
accs.add(new Account(accountName, Constants.ACCOUNT_TYPE_DEV));
|
||||||
|
Intent intent = AccountManager.newChooseAccountIntent(accs.get(0), accs, new String[]{Constants.ACCOUNT_TYPE_PROD, Constants.ACCOUNT_TYPE_DEV},
|
||||||
|
true, null, null, null, null);
|
||||||
|
activity.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.GET_ACCOUNTS},
|
||||||
|
// REQUEST_GET_ACCOUNTS_PERMISSION);
|
||||||
|
// Account ac = AccountImporter.getAccountForName(context, accountName);
|
||||||
|
// ArrayList<Account> li = new ArrayList<>();
|
||||||
|
// li.add(ac);
|
||||||
|
// Intent intent = AccountManager.newChooseAccountIntent(ac,
|
||||||
|
// li,
|
||||||
|
// new String[]{Constants.ACCOUNT_TYPE_PROD, Constants.ACCOUNT_TYPE_DEV},
|
||||||
|
// true,
|
||||||
|
// null,
|
||||||
|
// null,
|
||||||
|
// null,
|
||||||
|
// null);
|
||||||
|
// ((NotesListViewActivity) context).startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.remove("notes_last_etag");
|
||||||
|
editor.remove("notes_last_modified");
|
||||||
|
editor.remove("settingsUrl");
|
||||||
|
editor.remove("settingsUsername");
|
||||||
|
editor.remove("settingsPassword");
|
||||||
|
editor.apply();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// } catch (NextcloudFilesAppNotSupportedException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// } catch (NextcloudFilesAppAccountPermissionNotGrantedException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
} catch (AndroidGetAccountsPermissionNotGranted androidGetAccountsPermissionNotGranted) {
|
||||||
|
androidGetAccountsPermissionNotGranted.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,13 +98,7 @@ public class NoteServerSyncHelper {
|
||||||
private NoteServerSyncHelper(NoteSQLiteOpenHelper db) {
|
private NoteServerSyncHelper(NoteSQLiteOpenHelper db) {
|
||||||
this.dbHelper = db;
|
this.dbHelper = db;
|
||||||
this.appContext = db.getContext().getApplicationContext();
|
this.appContext = db.getContext().getApplicationContext();
|
||||||
try {
|
updateAccount();
|
||||||
this.localAccount = db.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(appContext).name);
|
|
||||||
notesClient = new NotesClient(appContext);
|
|
||||||
Log.v(getClass().getSimpleName(), "NextcloudRequest account: " + localAccount);
|
|
||||||
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
this.syncOnlyOnWifiKey = appContext.getResources().getString(R.string.pref_key_wifi_only);
|
this.syncOnlyOnWifiKey = appContext.getResources().getString(R.string.pref_key_wifi_only);
|
||||||
|
|
||||||
// Registers BroadcastReceiver to track network connection changes.
|
// Registers BroadcastReceiver to track network connection changes.
|
||||||
|
|
|
@ -76,23 +76,7 @@ public class NotesClient {
|
||||||
|
|
||||||
public NotesClient(Context context) {
|
public NotesClient(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
try {
|
updateAccount();
|
||||||
SingleSignOnAccount ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
|
|
||||||
Log.v(getClass().getSimpleName(), "NextcloudRequest account: " + ssoAccount.name);
|
|
||||||
mNextcloudAPI = new NextcloudAPI(context, ssoAccount, new GsonBuilder().create(), new NextcloudAPI.ApiConnectedListener() {
|
|
||||||
@Override
|
|
||||||
public void onConnected() {
|
|
||||||
Log.v(getClass().getSimpleName(), "SSO API connected");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateAccount() {
|
public void updateAccount() {
|
||||||
|
|
Loading…
Reference in a new issue