This commit is contained in:
Mario Đanić 2017-05-17 11:30:59 +00:00 committed by GitHub
commit c5af507080
9 changed files with 126 additions and 208 deletions

View file

@ -22,4 +22,8 @@ package com.owncloud.android.utils;
public class PushUtils {
public static final String KEY_PUSH = "push";
public static void pushRegistrationToServer() {
// do nothing
}
}

View file

@ -33,17 +33,6 @@
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".ui.activity.ModifiedFileDisplayActivity"
android:label="@string/app_name"
android:theme="@style/Theme.ownCloud.Toolbar.Drawer">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".authentication.ModifiedAuthenticatorActivity"
android:exported="true"
@ -70,15 +59,6 @@
</intent-filter>
</activity>
<activity
android:name=".ui.activity.FileDisplayActivity"
tools:node="remove"/>
<activity-alias
android:name=".ui.activity.FileDisplayActivity"
android:targetActivity=".ui.activity.ModifiedFileDisplayActivity"
tools:replace="android:targetActivity"/>
<activity-alias
android:name=".authentication.AuthenticatorActivity"
android:targetActivity=".authentication.ModifiedAuthenticatorActivity"

View file

@ -1,46 +0,0 @@
/**
* Nextcloud Android client application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.ui.activity;
import android.os.Bundle;
import com.owncloud.android.ui.events.TokenPushEvent;
import com.owncloud.android.utils.PushUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class ModifiedFileDisplayActivity extends FileDisplayActivity {
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(TokenPushEvent event) {
PushUtils.pushRegistrationToServer();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// see if there's stuff to push every time we start the app
EventBus.getDefault().post(new TokenPushEvent());
}
}

View file

@ -76,7 +76,7 @@ public class PushUtils {
private static ArbitraryDataProvider arbitraryDataProvider;
private static String generateSHA512Hash(String pushToken) {
public static String generateSHA512Hash(String pushToken) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("SHA-512");
@ -88,7 +88,7 @@ public class PushUtils {
return "";
}
private static String bytesToHex(byte[] bytes) {
public static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte individualByte : bytes) {
result.append(Integer.toString((individualByte & 0xff) + 0x100, 16)
@ -96,7 +96,7 @@ public class PushUtils {
}
return result.toString();
}
private static int generateRsa2048KeyPair() {
String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator
+ KEYPAIR_FOLDER;
@ -202,56 +202,63 @@ public class PushUtils {
Context context = MainApp.getAppContext();
String providerValue;
PushConfigurationState accountPushData = null;
Gson gson = new Gson();
for (Account account : AccountUtils.getAccounts(context)) {
if (!TextUtils.isEmpty(providerValue = arbitraryDataProvider.getValue(account, KEY_PUSH))) {
PushConfigurationState accountPushData = gson.fromJson(providerValue,
providerValue = arbitraryDataProvider.getValue(account, KEY_PUSH);
if (!TextUtils.isEmpty(providerValue)) {
accountPushData = gson.fromJson(providerValue,
PushConfigurationState.class);
if (!accountPushData.getPushToken().equals(token) && !accountPushData.isShouldBeDeleted()) {
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, context);
} else {
accountPushData = null;
}
RemoteOperation registerAccountDeviceForNotificationsOperation =
new RegisterAccountDeviceForNotificationsOperation(pushTokenHash,
publicKey,
context.getResources().getString(R.string.push_server_url));
if (accountPushData != null && !accountPushData.getPushToken().equals(token) &&
!accountPushData.isShouldBeDeleted() ||
TextUtils.isEmpty(providerValue)) {
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, context);
RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation.
execute(mClient);
RemoteOperation registerAccountDeviceForNotificationsOperation =
new RegisterAccountDeviceForNotificationsOperation(pushTokenHash,
publicKey,
context.getResources().getString(R.string.push_server_url));
RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation.
execute(mClient);
if (remoteOperationResult.isSuccess()) {
PushResponse pushResponse = remoteOperationResult.getPushResponseData();
RemoteOperation registerAccountDeviceForProxyOperation = new
RegisterAccountDeviceForProxyOperation(
context.getResources().getString(R.string.push_server_url),
token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey());
remoteOperationResult = registerAccountDeviceForProxyOperation.execute(mClient);
if (remoteOperationResult.isSuccess()) {
PushResponse pushResponse = remoteOperationResult.getPushResponseData();
RemoteOperation registerAccountDeviceForProxyOperation = new
RegisterAccountDeviceForProxyOperation(
context.getResources().getString(R.string.push_server_url),
token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey());
remoteOperationResult = registerAccountDeviceForProxyOperation.execute(mClient);
if (remoteOperationResult.isSuccess()) {
PushConfigurationState pushArbitraryData = new PushConfigurationState(token,
pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey(), false);
arbitraryDataProvider.storeOrUpdateKeyValue(account, KEY_PUSH,
gson.toJson(pushArbitraryData));
}
PushConfigurationState pushArbitraryData = new PushConfigurationState(token,
pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey(), false);
arbitraryDataProvider.storeOrUpdateKeyValue(account, KEY_PUSH,
gson.toJson(pushArbitraryData));
}
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
Log_OC.d(TAG, "Failed to find an account");
} catch (AuthenticatorException e) {
Log_OC.d(TAG, "Failed via AuthenticatorException");
} catch (IOException e) {
Log_OC.d(TAG, "Failed via IOException");
} catch (OperationCanceledException e) {
Log_OC.d(TAG, "Failed via OperationCanceledException");
}
} else if (accountPushData.isShouldBeDeleted()) {
deleteRegistrationForAccount(account);
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
Log_OC.d(TAG, "Failed to find an account");
} catch (AuthenticatorException e) {
Log_OC.d(TAG, "Failed via AuthenticatorException");
} catch (IOException e) {
Log_OC.d(TAG, "Failed via IOException");
} catch (OperationCanceledException e) {
Log_OC.d(TAG, "Failed via OperationCanceledException");
}
} else if (accountPushData != null && accountPushData.isShouldBeDeleted()) {
deleteRegistrationForAccount(account);
}
}
}
@ -261,7 +268,7 @@ public class PushUtils {
public static Key readKeyFromFile(boolean readPublicKey) {
String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator
+ KEYPAIR_FOLDER;
String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION;
String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION;

View file

@ -100,8 +100,11 @@ import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.ErrorMessageAdapter;
import com.owncloud.android.utils.MimeTypeUtil;
import com.owncloud.android.utils.PermissionUtil;
import com.owncloud.android.utils.PushUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.parceler.Parcels;
import java.io.File;
@ -2064,4 +2067,16 @@ public class FileDisplayActivity extends HookActivity
super.showFiles(onDeviceOnly);
getListOfFilesFragment().refreshDirectory();
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(TokenPushEvent event) {
PushUtils.pushRegistrationToServer();
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().post(new TokenPushEvent());
}
}

View file

@ -66,6 +66,8 @@ import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.PushUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.parceler.Parcels;
import butterknife.BindString;
@ -446,4 +448,9 @@ public class UserInfoActivity extends FileActivity {
outState.putParcelable(KEY_USER_DATA, Parcels.wrap(userInfo));
}
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(TokenPushEvent event) {
PushUtils.pushRegistrationToServer();
}
}

View file

@ -34,17 +34,6 @@
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".ui.activity.ModifiedFileDisplayActivity"
android:label="@string/app_name"
android:theme="@style/Theme.ownCloud.Toolbar.Drawer">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".authentication.ModifiedAuthenticatorActivity"
android:exported="true"
@ -71,15 +60,6 @@
</intent-filter>
</activity>
<activity
android:name=".ui.activity.FileDisplayActivity"
tools:node="remove"/>
<activity-alias
android:name=".ui.activity.FileDisplayActivity"
android:targetActivity=".ui.activity.ModifiedFileDisplayActivity"
tools:replace="android:targetActivity"/>
<activity-alias
android:name=".authentication.AuthenticatorActivity"
android:targetActivity=".authentication.ModifiedAuthenticatorActivity"

View file

@ -1,36 +0,0 @@
/**
* Nextcloud Android client application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.ui.activity;
import com.owncloud.android.ui.events.TokenPushEvent;
import com.owncloud.android.utils.PushUtils;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class ModifiedFileDisplayActivity extends FileDisplayActivity {
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(TokenPushEvent event) {
PushUtils.pushRegistrationToServer();
}
}

View file

@ -76,7 +76,7 @@ public class PushUtils {
private static ArbitraryDataProvider arbitraryDataProvider;
private static String generateSHA512Hash(String pushToken) {
public static String generateSHA512Hash(String pushToken) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("SHA-512");
@ -88,7 +88,7 @@ public class PushUtils {
return "";
}
private static String bytesToHex(byte[] bytes) {
public static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte individualByte : bytes) {
result.append(Integer.toString((individualByte & 0xff) + 0x100, 16)
@ -202,56 +202,63 @@ public class PushUtils {
Context context = MainApp.getAppContext();
String providerValue;
PushConfigurationState accountPushData = null;
Gson gson = new Gson();
for (Account account : AccountUtils.getAccounts(context)) {
if (!TextUtils.isEmpty(providerValue = arbitraryDataProvider.getValue(account, KEY_PUSH))) {
PushConfigurationState accountPushData = gson.fromJson(providerValue,
providerValue = arbitraryDataProvider.getValue(account, KEY_PUSH);
if (!TextUtils.isEmpty(providerValue)) {
accountPushData = gson.fromJson(providerValue,
PushConfigurationState.class);
if (!accountPushData.getPushToken().equals(token) && !accountPushData.isShouldBeDeleted()) {
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, context);
} else {
accountPushData = null;
}
RemoteOperation registerAccountDeviceForNotificationsOperation =
new RegisterAccountDeviceForNotificationsOperation(pushTokenHash,
publicKey,
context.getResources().getString(R.string.push_server_url));
if (accountPushData != null && !accountPushData.getPushToken().equals(token) &&
!accountPushData.isShouldBeDeleted() ||
TextUtils.isEmpty(providerValue)) {
try {
OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, context);
RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation.
execute(mClient);
RemoteOperation registerAccountDeviceForNotificationsOperation =
new RegisterAccountDeviceForNotificationsOperation(pushTokenHash,
publicKey,
context.getResources().getString(R.string.push_server_url));
RemoteOperationResult remoteOperationResult = registerAccountDeviceForNotificationsOperation.
execute(mClient);
if (remoteOperationResult.isSuccess()) {
PushResponse pushResponse = remoteOperationResult.getPushResponseData();
RemoteOperation registerAccountDeviceForProxyOperation = new
RegisterAccountDeviceForProxyOperation(
context.getResources().getString(R.string.push_server_url),
token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey());
remoteOperationResult = registerAccountDeviceForProxyOperation.execute(mClient);
if (remoteOperationResult.isSuccess()) {
PushResponse pushResponse = remoteOperationResult.getPushResponseData();
RemoteOperation registerAccountDeviceForProxyOperation = new
RegisterAccountDeviceForProxyOperation(
context.getResources().getString(R.string.push_server_url),
token, pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey());
remoteOperationResult = registerAccountDeviceForProxyOperation.execute(mClient);
if (remoteOperationResult.isSuccess()) {
PushConfigurationState pushArbitraryData = new PushConfigurationState(token,
pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey(), false);
arbitraryDataProvider.storeOrUpdateKeyValue(account, KEY_PUSH,
gson.toJson(pushArbitraryData));
}
PushConfigurationState pushArbitraryData = new PushConfigurationState(token,
pushResponse.getDeviceIdentifier(), pushResponse.getSignature(),
pushResponse.getPublicKey(), false);
arbitraryDataProvider.storeOrUpdateKeyValue(account, KEY_PUSH,
gson.toJson(pushArbitraryData));
}
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
Log_OC.d(TAG, "Failed to find an account");
} catch (AuthenticatorException e) {
Log_OC.d(TAG, "Failed via AuthenticatorException");
} catch (IOException e) {
Log_OC.d(TAG, "Failed via IOException");
} catch (OperationCanceledException e) {
Log_OC.d(TAG, "Failed via OperationCanceledException");
}
} else if (accountPushData.isShouldBeDeleted()) {
deleteRegistrationForAccount(account);
} catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
Log_OC.d(TAG, "Failed to find an account");
} catch (AuthenticatorException e) {
Log_OC.d(TAG, "Failed via AuthenticatorException");
} catch (IOException e) {
Log_OC.d(TAG, "Failed via IOException");
} catch (OperationCanceledException e) {
Log_OC.d(TAG, "Failed via OperationCanceledException");
}
} else if (accountPushData != null && accountPushData.isShouldBeDeleted()) {
deleteRegistrationForAccount(account);
}
}
}