use of android.os.AsyncTask in project AntennaPod by AntennaPod.
the class GpodnetAuthenticationActivity method setupLoginView.
private void setupLoginView(View view) {
final EditText username = (EditText) view.findViewById(R.id.etxtUsername);
final EditText password = (EditText) view.findViewById(R.id.etxtPassword);
final Button login = (Button) view.findViewById(R.id.butLogin);
final TextView txtvError = (TextView) view.findViewById(R.id.txtvError);
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progBarLogin);
password.setOnEditorActionListener((v, actionID, event) -> actionID == EditorInfo.IME_ACTION_GO && login.performClick());
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String usernameStr = username.getText().toString();
final String passwordStr = password.getText().toString();
if (BuildConfig.DEBUG)
Log.d(TAG, "Checking login credentials");
AsyncTask<GpodnetService, Void, Void> authTask = new AsyncTask<GpodnetService, Void, Void>() {
volatile Exception exception;
@Override
protected void onPreExecute() {
super.onPreExecute();
login.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
txtvError.setVisibility(View.GONE);
// hide the keyboard
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(login.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
login.setEnabled(true);
progressBar.setVisibility(View.GONE);
if (exception == null) {
advance();
} else {
txtvError.setText(exception.getCause().getMessage());
txtvError.setVisibility(View.VISIBLE);
}
}
@Override
protected Void doInBackground(GpodnetService... params) {
try {
params[0].authenticate(usernameStr, passwordStr);
GpodnetAuthenticationActivity.this.username = usernameStr;
GpodnetAuthenticationActivity.this.password = passwordStr;
} catch (GpodnetServiceException e) {
e.printStackTrace();
exception = e;
}
return null;
}
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
authTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, service);
} else {
authTask.execute();
}
}
});
}
use of android.os.AsyncTask in project XobotOS by xamarin.
the class ContentProvider method openPipeHelper.
/**
* A helper function for implementing {@link #openTypedAssetFile}, for
* creating a data pipe and background thread allowing you to stream
* generated data back to the client. This function returns a new
* ParcelFileDescriptor that should be returned to the caller (the caller
* is responsible for closing it).
*
* @param uri The URI whose data is to be written.
* @param mimeType The desired type of data to be written.
* @param opts Options supplied by caller.
* @param args Your own custom arguments.
* @param func Interface implementing the function that will actually
* stream the data.
* @return Returns a new ParcelFileDescriptor holding the read side of
* the pipe. This should be returned to the caller for reading; the caller
* is responsible for closing it when done.
*/
public <T> ParcelFileDescriptor openPipeHelper(final Uri uri, final String mimeType, final Bundle opts, final T args, final PipeDataWriter<T> func) throws FileNotFoundException {
try {
final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
func.writeDataToPipe(fds[1], uri, mimeType, opts, args);
try {
fds[1].close();
} catch (IOException e) {
Log.w(TAG, "Failure closing pipe", e);
}
return null;
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[]) null);
return fds[0];
} catch (IOException e) {
throw new FileNotFoundException("failure making pipe");
}
}
use of android.os.AsyncTask in project android_frameworks_base by AOSPA.
the class LockPatternChecker method verifyPattern.
/**
* Verify a pattern asynchronously.
*
* @param utils The LockPatternUtils instance to use.
* @param pattern The pattern to check.
* @param challenge The challenge to verify against the pattern.
* @param userId The user to check against the pattern.
* @param callback The callback to be invoked with the verification result.
*/
public static AsyncTask<?, ?, ?> verifyPattern(final LockPatternUtils utils, final List<LockPatternView.Cell> pattern, final long challenge, final int userId, final OnVerifyCallback callback) {
AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
private int mThrottleTimeout;
private List<LockPatternView.Cell> patternCopy;
@Override
protected void onPreExecute() {
// Make a copy of the pattern to prevent race conditions.
// No need to clone the individual cells because they are immutable.
patternCopy = new ArrayList(pattern);
}
@Override
protected byte[] doInBackground(Void... args) {
try {
return utils.verifyPattern(patternCopy, challenge, userId);
} catch (RequestThrottledException ex) {
mThrottleTimeout = ex.getTimeoutMs();
return null;
}
}
@Override
protected void onPostExecute(byte[] result) {
callback.onVerified(result, mThrottleTimeout);
}
};
task.execute();
return task;
}
use of android.os.AsyncTask in project android_frameworks_base by AOSPA.
the class LockPatternChecker method checkPattern.
/**
* Checks a pattern asynchronously.
*
* @param utils The LockPatternUtils instance to use.
* @param pattern The pattern to check.
* @param userId The user to check against the pattern.
* @param callback The callback to be invoked with the check result.
*/
public static AsyncTask<?, ?, ?> checkPattern(final LockPatternUtils utils, final List<LockPatternView.Cell> pattern, final int userId, final OnCheckCallback callback) {
AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
private int mThrottleTimeout;
private List<LockPatternView.Cell> patternCopy;
@Override
protected void onPreExecute() {
// Make a copy of the pattern to prevent race conditions.
// No need to clone the individual cells because they are immutable.
patternCopy = new ArrayList(pattern);
}
@Override
protected Boolean doInBackground(Void... args) {
try {
return utils.checkPattern(patternCopy, userId, callback::onEarlyMatched);
} catch (RequestThrottledException ex) {
mThrottleTimeout = ex.getTimeoutMs();
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
callback.onChecked(result, mThrottleTimeout);
}
};
task.execute();
return task;
}
use of android.os.AsyncTask in project Shuttle by timusus.
the class SettingsFragment method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().supportInvalidateOptionsMenu();
mTintManager = new SystemBarTintManager(getActivity());
if (getArguments() != null) {
mPrefResId = getArguments().getInt(PREF_RES_ID);
} else {
mPrefResId = R.xml.settings_headers;
}
// Load the preferences from an XML resource
addPreferencesFromResource(mPrefResId);
mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
final Preference chooseTabsPreference = findPreference("pref_tab_chooser");
if (chooseTabsPreference != null) {
chooseTabsPreference.setOnPreferenceClickListener(preference -> {
RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.dialog_tab_chooser, null);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
TabsAdapter tabsAdapter = new TabsAdapter(getContext());
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(tabsAdapter::moveItem, (fromPosition, toPosition) -> tabsAdapter.updatePreferences(), () -> {
}));
itemTouchHelper.attachToRecyclerView(recyclerView);
tabsAdapter.setListener(new TabsAdapter.TabListener() {
@Override
public void onItemClick(View v, int position, CategoryItem categoryItem) {
categoryItem.setChecked(!categoryItem.isChecked());
com.simplecity.amp_library.utils.AnalyticsManager.logTabVisibilityChanged(categoryItem.isChecked(), categoryItem.title);
tabsAdapter.notifyItemChanged(position);
tabsAdapter.updatePreferences();
}
@Override
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
itemTouchHelper.startDrag(viewHolder);
}
});
recyclerView.setAdapter(tabsAdapter);
DialogUtils.getBuilder(getContext()).title(R.string.pref_title_choose_tabs).customView(recyclerView, false).positiveText(R.string.button_done).onPositive((materialDialog, dialogAction) -> {
DialogUtils.createRestartDialog(getActivity());
materialDialog.dismiss();
}).show();
return true;
});
}
final Preference colorPickerPreference = findPreference("pref_theme_highlight_color");
if (colorPickerPreference != null) {
colorPickerPreference.setOnPreferenceClickListener(preference -> {
int selectedColor = mPrefs.getInt("pref_theme_highlight_color", -1);
DialogUtils.showColorPickerDialog(this, selectedColor, color -> mPrefs.edit().putInt("pref_theme_highlight_color", color).apply());
return true;
});
}
final Preference accentPickerPreference = findPreference("pref_theme_accent_color");
if (accentPickerPreference != null) {
accentPickerPreference.setOnPreferenceClickListener(preference -> {
int selectedColor = mPrefs.getInt("pref_theme_accent_color", -1);
DialogUtils.showColorPickerDialog(this, selectedColor, ColorPalette.getAccentColors(), ColorPalette.getAccentColorsSub(), color -> mPrefs.edit().putInt("pref_theme_accent_color", color).apply());
return true;
});
}
mListener = (sharedPreferences, key) -> {
if (key.equals("pref_theme_highlight_color") || key.equals("pref_theme_accent_color") || key.equals("pref_theme_white_accent")) {
ThemeUtils.setTheme(getActivity());
ThemeUtils.themeActionBar((SettingsActivity) getActivity());
ThemeUtils.themeStatusBar(getActivity(), mTintManager);
getListView().invalidate();
themeUIElements();
for (int i = 0, size = getListView().getChildCount(); i < size; i++) {
View view = getListView().getChildAt(i);
ThemeUtils.updateThemableViews(view);
}
}
if (key.equals("pref_theme_base") || key.equals("pref_default_page")) {
DialogUtils.createRestartDialog(getActivity());
}
};
final Preference restartPreference = findPreference("pref_restart");
if (restartPreference != null) {
restartPreference.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(getActivity(), MainActivity.class);
ComponentName componentNAme = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(componentNAme);
startActivity(mainIntent);
return true;
});
}
final CheckBoxPreference showLockscreenArtworkPreference = (CheckBoxPreference) findPreference(SettingsManager.KEY_SHOW_LOCKSCREEN_ARTWORK);
if (showLockscreenArtworkPreference != null) {
showLockscreenArtworkPreference.setOnPreferenceClickListener(preference -> {
MusicUtils.toggleLockscreenArtwork();
return false;
});
}
final Preference downloadArtworkPreference = findPreference("pref_download_artwork");
if (downloadArtworkPreference != null) {
downloadArtworkPreference.setOnPreferenceClickListener(preference -> {
DialogUtils.showDownloadWarningDialog(getActivity(), (materialDialog, dialogAction) -> {
Intent intent = new Intent(getContext(), ArtworkDownloadService.class);
ShuttleApplication.getInstance().startService(intent);
});
return true;
});
}
final Preference deleteArtworkPreference = findPreference("pref_delete_artwork");
if (deleteArtworkPreference != null) {
deleteArtworkPreference.setOnPreferenceClickListener(preference -> {
DialogUtils.getBuilder(getActivity()).title(getString(R.string.pref_title_delete_artwork)).icon(DrawableUtils.themeLightOrDark(getActivity(), getResources().getDrawable(R.drawable.ic_dialog_alert))).content(getString(R.string.delete_artwork_confirmation_dialog)).positiveText(getString(R.string.button_ok)).onPositive((materialDialog, dialogAction) -> {
Glide.get(getContext()).clearMemory();
ShuttleUtils.execute(new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Glide.get(getContext()).clearDiskCache();
return null;
}
});
}).negativeText(getString(R.string.cancel)).show();
return true;
});
}
final Preference downloadSimpleLastFmScrobbler = findPreference("pref_download_simple_lastfm_scrobbler");
if (downloadSimpleLastFmScrobbler != null) {
if (ShuttleUtils.isAmazonBuild()) {
PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("pref_key_simple_lastfm_scrobble_settings");
if (preferenceGroup != null) {
preferenceGroup.removePreference(downloadSimpleLastFmScrobbler);
}
} else {
downloadSimpleLastFmScrobbler.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adam.aslfms")));
}
}
final Preference about = findPreference("pref_about");
if (about != null) {
about.setOnPreferenceClickListener(preference -> {
View customView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_changelog, null);
WebView webView = (WebView) customView.findViewById(R.id.webView);
int themeType = ThemeUtils.getThemeType(getActivity());
webView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
if (themeType == ThemeUtils.ThemeType.TYPE_LIGHT || themeType == ThemeUtils.ThemeType.TYPE_SOLID_LIGHT) {
webView.loadUrl("file:///android_asset/web/info.html");
} else {
webView.loadUrl("file:///android_asset/web/info_dark.html");
}
DialogUtils.getBuilder(getActivity()).title(R.string.pref_title_about).customView(customView, false).negativeText(R.string.close).show();
AnalyticsManager.logChangelogViewed();
return true;
});
}
final Preference upgrade = findPreference("pref_upgrade");
if (upgrade != null) {
if (ShuttleUtils.isUpgraded()) {
SettingsFragment.this.getPreferenceScreen().removePreference(upgrade);
}
upgrade.setOnPreferenceClickListener(preference -> {
DialogUtils.showUpgradeDialog(getActivity(), (materialDialog, dialogAction) -> {
if (ShuttleUtils.isAmazonBuild()) {
ShuttleUtils.openShuttleLink(getActivity(), "com.simplecity.amp_pro");
} else {
AnalyticsManager.logUpgrade(AnalyticsManager.UpgradeType.UPGRADE);
((SettingsActivity) getActivity()).purchasePremiumUpgrade();
}
});
return true;
});
}
final Preference viewBlacklist = findPreference("pref_blacklist_view");
if (viewBlacklist != null) {
viewBlacklist.setOnPreferenceClickListener(preference -> {
DialogUtils.showBlacklistDialog(getActivity());
return true;
});
}
final Preference viewWhitelist = findPreference("pref_whitelist_view");
if (viewWhitelist != null) {
viewWhitelist.setOnPreferenceClickListener(preference -> {
DialogUtils.showWhitelistDialog(getActivity());
return true;
});
}
final Preference clearBlacklist = findPreference("pref_blacklist_clear");
if (clearBlacklist != null) {
clearBlacklist.setOnPreferenceClickListener(preference -> {
BlacklistHelper.deleteAllSongs();
Toast.makeText(getActivity(), R.string.blacklist_deleted, Toast.LENGTH_SHORT).show();
return true;
});
}
final Preference clearWhitelist = findPreference("pref_whitelist_clear");
if (clearWhitelist != null) {
clearWhitelist.setOnPreferenceClickListener(preference -> {
WhitelistHelper.deleteAllFolders();
Toast.makeText(getActivity(), R.string.whitelist_deleted, Toast.LENGTH_SHORT).show();
return true;
});
}
final CheckBoxPreference ignoreEmbeddedArtwork = (CheckBoxPreference) findPreference(SettingsManager.KEY_IGNORE_EMBEDDED_ARTWORK);
if (ignoreEmbeddedArtwork != null) {
ignoreEmbeddedArtwork.setOnPreferenceChangeListener((preference, newValue) -> {
showArtworkPreferenceDialog();
return true;
});
}
final CheckBoxPreference ignoreFolderArtwork = (CheckBoxPreference) findPreference(SettingsManager.KEY_IGNORE_FOLDER_ARTWORK);
if (ignoreFolderArtwork != null) {
ignoreFolderArtwork.setOnPreferenceChangeListener((preference, newValue) -> {
showArtworkPreferenceDialog();
return true;
});
}
final CheckBoxPreference preferEmbeddedArtwork = (CheckBoxPreference) findPreference(SettingsManager.KEY_PREFER_EMBEDDED_ARTWORK);
if (preferEmbeddedArtwork != null) {
preferEmbeddedArtwork.setOnPreferenceChangeListener((preference, newValue) -> {
showArtworkPreferenceDialog();
return true;
});
}
final CheckBoxPreference ignoreMediaStoreArtwork = (CheckBoxPreference) findPreference(SettingsManager.KEY_IGNORE_MEDIASTORE_ART);
if (ignoreMediaStoreArtwork != null) {
ignoreMediaStoreArtwork.setOnPreferenceChangeListener((preference, newValue) -> {
showArtworkPreferenceDialog();
return true;
});
}
final CheckBoxPreference preferLastFmArtwork = (CheckBoxPreference) findPreference(SettingsManager.KEY_PREFER_LAST_FM);
if (preferLastFmArtwork != null) {
preferLastFmArtwork.setOnPreferenceChangeListener((preference, newValue) -> {
showArtworkPreferenceDialog();
return true;
});
}
final Preference restorePurchases = findPreference("pref_restore_purchases");
if (ShuttleUtils.isAmazonBuild() || ShuttleUtils.isUpgraded()) {
PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("support_group");
if (preferenceGroup != null) {
preferenceGroup.removePreference(restorePurchases);
}
} else if (restorePurchases != null) {
restorePurchases.setOnPreferenceClickListener(preference -> {
((SettingsActivity) getActivity()).restorePurchases();
return true;
});
}
final Preference versionPreference = findPreference("pref_version");
if (versionPreference != null) {
versionPreference.setSummary("Shuttle Music Player " + BuildConfig.VERSION_NAME + (ShuttleUtils.isUpgraded() ? " (Upgraded)" : " (Free)"));
}
final Preference faqPreference = findPreference("pref_faq");
if (faqPreference != null) {
faqPreference.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.shuttlemusicplayer.com/#faq"));
startActivity(intent);
return true;
});
}
final Preference gplusPreference = findPreference("pref_gplus");
if (gplusPreference != null) {
gplusPreference.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/communities/112365043563095486408"));
startActivity(intent);
return true;
});
}
final Preference ratePreference = findPreference("pref_rate");
if (ratePreference != null) {
ratePreference.setOnPreferenceClickListener(preference -> {
final String appPackageName = getActivity().getPackageName();
ShuttleUtils.openShuttleLink(getActivity(), appPackageName);
SettingsManager.getInstance().setHasRated();
return true;
});
}
final CheckBoxPreference openOnClickPreference = (CheckBoxPreference) findPreference("pref_open_now_playing_on_click");
if (openOnClickPreference != null) {
if (!ShuttleUtils.isTablet()) {
PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("display_group");
if (preferenceGroup != null) {
preferenceGroup.removePreference(openOnClickPreference);
}
}
}
if (!ShuttleUtils.hasLollipop()) {
PreferenceScreen preferenceScreen = getPreferenceScreen();
if (preferenceScreen != null) {
Preference notificationPreference = preferenceScreen.findPreference("pref_category_notifications");
if (notificationPreference != null) {
preferenceScreen.removePreference(notificationPreference);
}
}
}
}
Aggregations