From fffbd2b15d1d515771a4d496b1c7cccc97c4bc5b Mon Sep 17 00:00:00 2001 From: stefan-niedermann Date: Thu, 10 Oct 2019 13:51:06 +0200 Subject: [PATCH] Ask to import account on migration --- .../activity/NotesListViewActivity.java | 20 ++--- .../persistence/NoteSQLiteOpenHelper.java | 90 +++++++++++++++++-- .../persistence/NoteServerSyncHelper.java | 8 +- .../owncloud/notes/util/NotesClient.java | 18 +--- 4 files changed, 95 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java index eae160fd..7cf17572 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/android/activity/NotesListViewActivity.java @@ -200,9 +200,7 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap try { // to get current account from SingleAccountHelper selectAccount(SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()).name); Log.v(getClass().getSimpleName(), "NextcloudRequest account: " + localAccount); - } catch (NextcloudFilesAppAccountNotFoundException e) { - e.printStackTrace(); - } catch (NoCurrentAccountSelectedException e) { + } catch (NoCurrentAccountSelectedException | NextcloudFilesAppAccountNotFoundException e) { if (db.hasAccounts()) { // If nothing is stored in SingleAccountHelper, check db for accounts selectAccount(db.getAccounts().get(0).getAccountName()); } else { @@ -740,14 +738,6 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap protected void onActivityResult(int requestCode, int resultCode, Intent 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 if (requestCode == create_note_cmd) { // 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. // @see https://github.com/stefan-niedermann/nextcloud-notes/issues/529 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); + }); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java index c7241574..8da56ebc 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteSQLiteOpenHelper.java @@ -1,9 +1,14 @@ 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.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.content.pm.ShortcutManager; import android.content.res.Resources; import android.database.Cursor; @@ -18,7 +23,13 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; 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.Calendar; import java.util.Collections; @@ -27,6 +38,7 @@ import java.util.List; import java.util.Map; 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.SingleNoteWidget; 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.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. */ @@ -72,13 +86,13 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper { private NoteSQLiteOpenHelper(Context context) { super(context, database_name, null, database_version); - this.context = context.getApplicationContext(); + this.context = context; serverSyncHelper = NoteServerSyncHelper.getInstance(this); } public static NoteSQLiteOpenHelper getInstance(Context context) { if (instance == null) - return instance = new NoteSQLiteOpenHelper(context.getApplicationContext()); + return instance = new NoteSQLiteOpenHelper(context); else return instance; } @@ -163,10 +177,74 @@ public class NoteSQLiteOpenHelper extends SQLiteOpenHelper { values.put(key_account_id, 1); db.update(table_notes, values, key_account_id + " = ?", new String[]{"NULL"}); - SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); - editor.remove("notes_last_etag"); - editor.remove("notes_last_modified"); - editor.apply(); + + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + + 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 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 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(); + } + } } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java index 79f84dc6..ca6a2d72 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java @@ -98,13 +98,7 @@ public class NoteServerSyncHelper { private NoteServerSyncHelper(NoteSQLiteOpenHelper db) { this.dbHelper = db; this.appContext = db.getContext().getApplicationContext(); - try { - 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(); - } + updateAccount(); this.syncOnlyOnWifiKey = appContext.getResources().getString(R.string.pref_key_wifi_only); // Registers BroadcastReceiver to track network connection changes. diff --git a/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClient.java b/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClient.java index ba5c8c04..f5cee393 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClient.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/util/NotesClient.java @@ -76,23 +76,7 @@ public class NotesClient { public NotesClient(Context context) { this.context = context; - try { - 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(); - } + updateAccount(); } public void updateAccount() {