-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add calendar and account, change menu for reminders, add services
- Loading branch information
Showing
34 changed files
with
1,662 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/account/AccountResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package com.kunzisoft.remembirthday.account; | ||
|
||
import android.Manifest; | ||
import android.accounts.Account; | ||
import android.accounts.AccountManager; | ||
import android.accounts.AccountManagerFuture; | ||
import android.app.AlarmManager; | ||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Messenger; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.util.Log; | ||
|
||
import com.kunzisoft.remembirthday.service.MainIntentService; | ||
|
||
/** | ||
* Created by joker on 25/07/17. | ||
*/ | ||
public class AccountResolver { | ||
|
||
private Context context; | ||
private Handler backgroundStatusHandler; | ||
private String name; | ||
private String type; | ||
private Account account; | ||
private String authority; | ||
|
||
public AccountResolver(Context context, | ||
String name, | ||
String type, | ||
String authority, | ||
Handler handler) { | ||
this.context = context; | ||
this.name = name; | ||
this.type = type; | ||
this.account = new Account(name, type); | ||
this.authority = authority; | ||
this.backgroundStatusHandler = handler; | ||
} | ||
|
||
public AccountResolver(Context context, | ||
String accountName, | ||
String type, | ||
String authority) { | ||
this(context, accountName, type, authority, null); | ||
} | ||
|
||
/** | ||
* Add account for Birthday Adapter to Android system | ||
*/ | ||
public Bundle addAccountAndSync() { | ||
Log.d(getClass().getSimpleName(), "Adding calendar account : " + account.name); | ||
|
||
// enable automatic sync once per day | ||
ContentResolver.setSyncAutomatically(account, authority, true); | ||
ContentResolver.setIsSyncable(account, type, 1); | ||
|
||
// add periodic sync interval once per day | ||
long freq = AlarmManager.INTERVAL_DAY; | ||
ContentResolver.addPeriodicSync(account, type, new Bundle(), freq); | ||
|
||
AccountManager accountManager = AccountManager.get(context); | ||
if (accountManager.addAccountExplicitly(account, null, null)) { | ||
Bundle result = new Bundle(); | ||
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); | ||
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); | ||
|
||
// Force a sync! Even when background sync is disabled, this will force one sync! | ||
manualSync(); | ||
|
||
return result; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Remove account from Android system | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public boolean removeAccount() { | ||
Log.d(getClass().getSimpleName(), "Removing account : " + account.name); | ||
|
||
AccountManager accountManager = AccountManager.get(context); | ||
// remove account | ||
AccountManagerFuture accountManagerFuture; | ||
if(android.os.Build.VERSION.SDK_INT < 23) { | ||
accountManagerFuture = accountManager.removeAccount(account, null, null); | ||
} else { | ||
accountManagerFuture = accountManager.removeAccount(account, null, null, null); | ||
} | ||
if (accountManagerFuture.isDone()) { | ||
try { | ||
accountManagerFuture.getResult(); | ||
return true; | ||
} catch (Exception e) { | ||
Log.e(getClass().getSimpleName(), "Problem while removing account!", e); | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Force a manual sync now! | ||
*/ | ||
public void manualSync() { | ||
Log.d(getClass().getSimpleName(), "Force manual synchronisation"); | ||
|
||
// Disabled: Force resync in Android OS | ||
// Bundle extras = new Bundle(); | ||
// extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); | ||
// ContentResolver.requestSync(Constants.ACCOUNT, Constants.CONTENT_AUTHORITY, extras); | ||
|
||
// Enabled: Force resync in own thread: | ||
// Send all information needed to service to do in other thread | ||
//TODO Service | ||
Intent intent = new Intent(context, MainIntentService.class); | ||
|
||
// Create a new Messenger for the communication back | ||
if (backgroundStatusHandler != null) { | ||
Messenger messenger = new Messenger(backgroundStatusHandler); | ||
intent.putExtra(MainIntentService.EXTRA_MESSENGER, messenger); | ||
} | ||
intent.setAction(MainIntentService.ACTION_MANUAL_COMPLETE_SYNC); | ||
|
||
// start service with intent | ||
context.startService(intent); | ||
} | ||
|
||
/** | ||
* Checks whether the account is enabled or not | ||
*/ | ||
public static boolean isAccountActivated(Context context, String type, String name) { | ||
AccountManager accountManager = AccountManager.get(context); | ||
if (ActivityCompat.checkSelfPermission(context, | ||
Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) { | ||
return false; | ||
} | ||
Account[] availableAccounts = accountManager.getAccountsByType(type); | ||
for (Account currentAccount : availableAccounts) { | ||
if (currentAccount.name.equals(name)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/* | ||
@OnShowRationale(Manifest.permission.GET_ACCOUNTS) | ||
void showRationaleForGetAccounts(final PermissionRequest request) { | ||
new AlertDialog.Builder(context) | ||
.setMessage(R.string.permission_get_accounts_rationale) | ||
.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
request.proceed(); | ||
} | ||
}) | ||
.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialogInterface, int i) { | ||
request.cancel(); | ||
} | ||
}) | ||
.show(); | ||
} | ||
@OnPermissionDenied(Manifest.permission.GET_ACCOUNTS) | ||
void showDeniedForGetAccounts() { | ||
Toast.makeText(context, R.string.permission_get_accounts_denied, Toast.LENGTH_LONG).show(); | ||
} | ||
@OnNeverAskAgain(Manifest.permission.GET_ACCOUNTS) | ||
void showNeverAskForGetAccounts() { | ||
Toast.makeText(context, R.string.permission_get_accounts_never_ask, Toast.LENGTH_LONG).show(); | ||
} | ||
*/ | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public Account getAccount() { | ||
return account; | ||
} | ||
|
||
public String getAuthority() { | ||
return authority; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...irthday-UI/src/main/java/com/kunzisoft/remembirthday/account/BackgroundStatusHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2012-2013 Dominik Schürmann <[email protected]> | ||
* | ||
* This file is part of Birthday Adapter. | ||
* | ||
* Birthday Adapter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Birthday Adapter 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 General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Birthday Adapter. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package com.kunzisoft.remembirthday.account; | ||
|
||
import android.os.Handler; | ||
import android.os.Message; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
public class BackgroundStatusHandler extends Handler { | ||
|
||
public static final int BACKGROUND_STATUS_HANDLER_DISABLE = 0; | ||
public static final int BACKGROUND_STATUS_HANDLER_ENABLE = 1; | ||
|
||
private WeakReference<StatusChangeListener> mListener; | ||
|
||
public BackgroundStatusHandler(StatusChangeListener activity) { | ||
mListener = new WeakReference<>(activity); | ||
noOfRunningBackgroundThreads = 0; | ||
} | ||
|
||
private int noOfRunningBackgroundThreads; | ||
|
||
@Override | ||
public void handleMessage(Message msg) { | ||
StatusChangeListener listener = mListener.get(); | ||
final int what = msg.what; | ||
|
||
switch (what) { | ||
case BACKGROUND_STATUS_HANDLER_ENABLE: | ||
noOfRunningBackgroundThreads++; | ||
|
||
if (listener != null) { | ||
listener.onStatusChange(true); | ||
} | ||
break; | ||
|
||
case BACKGROUND_STATUS_HANDLER_DISABLE: | ||
noOfRunningBackgroundThreads--; | ||
|
||
if (noOfRunningBackgroundThreads <= 0) { | ||
if (listener != null) { | ||
listener.onStatusChange(false); | ||
} | ||
} | ||
|
||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
public interface StatusChangeListener { | ||
public void onStatusChange(boolean progress); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
RememBirthday-UI/src/main/java/com/kunzisoft/remembirthday/account/CalendarAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.kunzisoft.remembirthday.account; | ||
|
||
import android.content.Context; | ||
import android.os.Handler; | ||
|
||
import com.kunzisoft.remembirthday.R; | ||
|
||
/** | ||
* Created by joker on 25/07/17. | ||
*/ | ||
|
||
public class CalendarAccount { | ||
|
||
private CalendarAccount() {} | ||
|
||
public static AccountResolver getAccount(Context context) { | ||
return getAccount(context, null); | ||
} | ||
|
||
public static AccountResolver getAccount(Context context, Handler handler) { | ||
return new AccountResolver(context, | ||
getAccountName(context), | ||
getAccountType(context), | ||
getAccountAuthority(context), | ||
handler); | ||
} | ||
|
||
public static boolean isAccountActivated(Context context) { | ||
return AccountResolver.isAccountActivated(context, | ||
getAccountType(context), | ||
getAccountName(context)); | ||
} | ||
|
||
public static String getAccountName(Context context) { | ||
return context.getString(R.string.account_name); | ||
} | ||
|
||
public static String getAccountType(Context context) { | ||
return context.getString(R.string.account_type); | ||
} | ||
|
||
public static String getAccountAuthority(Context context) { | ||
return context.getString(R.string.account_authority); | ||
} | ||
} |
Oops, something went wrong.