下面列出了android.content.res.Resources#getText() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static CharSequence getResText(int resourceId) {
Context context = CoreMaterialApplication.getContext();
if (context == null) {
return null;
}
Resources resources = context.getResources();
if (resources == null) {
return null;
}
return resources.getText(resourceId);
}
@Provides
SearchPlaybackInitializer provideSearchPlaybackInitializer(
@NonNull final Resources resources,
@NonNull final PlaybackInitializer playbackInitializer,
@NonNull final PlaybackServiceControl playbackServiceControl,
@NonNull final QueueProviderSearch queueProviderSearch) {
return new SearchPlaybackInitializerImpl(
resources.getText(R.string.No_media_found),
resources.getString(R.string.No_media_found_for_s),
playbackInitializer,
playbackServiceControl,
queueProviderSearch);
}
/**
* Return the currently set summary. If {@link #summaryRes} is set,
* this resource is loaded from <var>res</var> and returned. Otherwise
* {@link #summary} is returned.
*/
public CharSequence getSummary(Resources res) {
if (summaryRes != 0) {
return res.getText(summaryRes);
}
return summary;
}
/**
* Return the currently set summary. If {@link #summaryRes} is set,
* this resource is loaded from <var>res</var> and returned. Otherwise {@link #summary} is
* returned.
*/
public CharSequence getSummary(Resources res) {
if (summaryRes != 0) {
return res.getText(summaryRes);
}
return summary;
}
/**
* Return the currently set bread crumb title. If {@link #breadCrumbTitleRes} is set,
* this resource is loaded from <var>res</var> and returned. Otherwise
* {@link #breadCrumbTitle} is returned.
*/
public CharSequence getBreadCrumbTitle(Resources res) {
if (breadCrumbTitleRes != 0) {
return res.getText(breadCrumbTitleRes);
}
return breadCrumbTitle;
}
private void setHeaderInternal(final int titleRes, final CharSequence title, final int iconRes,
final Drawable icon, final View view) {
final Resources r = getResources();
if (view != null) {
mHeaderView = view;
// If using a custom view, then the title and icon aren't used
mHeaderTitle = null;
mHeaderIcon = null;
} else {
if (titleRes > 0) {
mHeaderTitle = r.getText(titleRes);
} else if (title != null) {
mHeaderTitle = title;
}
if (iconRes > 0) {
mHeaderIcon = r.getDrawable(iconRes);
} else if (icon != null) {
mHeaderIcon = icon;
}
// If using the title or icon, then a custom view isn't used
mHeaderView = null;
}
// Notify of change
onItemsChanged(false);
}
/**
* This looks at the ime options given by the current editor, to set the
* appropriate label on the keyboard's enter key (if it has one).
*/
void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
int valnorm = KeyEvent.KEYCODE_ENTER;
switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = res.getText(R.string.label_go_key);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = res.getText(R.string.label_next_key);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.codes = NORMAL_ENTER;
mEnterKey.label = res.getText(R.string.label_send_key);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
mEnterKey.label = null;
mEnterKey.codes = TERMINAL_ENTER;
break;
}
}
/**
* Return the currently set title. If {@link #titleRes} is set,
* this resource is loaded from <var>res</var> and stored in {@link #title}.
*/
public CharSequence getTitle(Resources res) {
if (titleRes != 0) {
title = res.getText(titleRes);
titleRes = 0;
}
return title;
}
/**
* This looks at the ime options given by the current editor, to set the
* appropriate label on the keyboard's enter key (if it has one).
*/
void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_go_key);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_next_key);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.ic_search_45dp);
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_send_key);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.ic_check_circle_45dp);
mEnterKey.label = null;
break;
}
}
/**
* Returns summary string.
*/
public static String generateSummary(Resources resources) {
if (DataReductionProxySettings.getInstance().isDataReductionProxyEnabled()) {
String percent = DataReductionProxySettings.getInstance()
.getContentLengthPercentSavings();
return resources.getString(
R.string.data_reduction_menu_item_summary, percent);
} else {
return (String) resources.getText(R.string.text_off);
}
}
/**
* Returns summary string.
*/
public static String generateSummary(Resources resources) {
if (DataReductionProxySettings.getInstance().isDataReductionProxyEnabled()) {
String percent = DataReductionProxySettings.getInstance()
.getContentLengthPercentSavings();
return resources.getString(
R.string.data_reduction_menu_item_summary, percent);
} else {
return (String) resources.getText(R.string.text_off);
}
}
/**
* Returns summary string.
*/
public static String generateSummary(Resources resources) {
if (DataReductionProxySettings.getInstance().isDataReductionProxyEnabled()) {
String percent = DataReductionProxySettings.getInstance()
.getContentLengthPercentSavings();
return resources.getString(
R.string.data_reduction_menu_item_summary, percent);
} else {
return (String) resources.getText(R.string.text_off);
}
}
public void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_go);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_next);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_send);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
mEnterKey.label = null;
break;
}
}
/**
* Return the currently set bread crumb short title. If {@link #breadCrumbShortTitleRes} is set,
* this resource is loaded from <var>res</var> and returned. Otherwise {@link #breadCrumbShortTitle} is returned.
*/
public CharSequence getBreadCrumbShortTitle(Resources res)
{
if (breadCrumbShortTitleRes != 0)
{
return res.getText(breadCrumbShortTitleRes);
}
return breadCrumbShortTitle;
}
/**
* Return the currently set summary. If {@link #summaryRes} is set,
* this resource is loaded from <var>res</var> and returned. Otherwise
* {@link #summary} is returned.
*/
public CharSequence getSummary(Resources res) {
if (summaryRes != 0) {
return res.getText(summaryRes);
}
return summary;
}
private void setHeaderInternal(final int titleRes, final CharSequence title, final int iconRes,
final Drawable icon, final View view) {
final Resources r = getResources();
if (view != null) {
mHeaderView = view;
// If using a custom view, then the title and icon aren't used
mHeaderTitle = null;
mHeaderIcon = null;
} else {
if (titleRes > 0) {
mHeaderTitle = r.getText(titleRes);
} else if (title != null) {
mHeaderTitle = title;
}
if (iconRes > 0) {
mHeaderIcon = r.getDrawable(iconRes);
} else if (icon != null) {
mHeaderIcon = icon;
}
// If using the title or icon, then a custom view isn't used
mHeaderView = null;
}
// Notify of change
onItemsChanged(false);
}
public void setImeOptions(Resources res, int options) {
if (mEnterKey == null) {
return;
}
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
case EditorInfo.IME_ACTION_GO:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_go);
break;
case EditorInfo.IME_ACTION_NEXT:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_next);
break;
case EditorInfo.IME_ACTION_SEARCH:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
mEnterKey.label = null;
break;
case EditorInfo.IME_ACTION_SEND:
mEnterKey.iconPreview = null;
mEnterKey.icon = null;
mEnterKey.label = res.getText(R.string.label_keyboard_key_send);
break;
default:
mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
mEnterKey.label = null;
break;
}
}
public CharSequence getTimeText(Resources res, boolean showEmptyText) {
if (isRunning()) {
return res.getText(R.string.app_ops_running);
}
if (getTime() > 0) {
return DateUtils.getRelativeTimeSpanString(getTime(),
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE);
}
return showEmptyText ? res.getText(R.string.app_ops_never_used) : "";
}
/**
* Update all active widget instances by pushing changes
*/
private void performUpdate(Context context, DownloadService service, int[] appWidgetIds, boolean playing) {
final Resources res = context.getResources();
final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout());
if(playing) {
views.setViewVisibility(R.id.widget_root, View.VISIBLE);
} else {
// Hide widget
SharedPreferences prefs = Util.getPreferences(context);
if(prefs.getBoolean(Constants.PREFERENCES_KEY_HIDE_WIDGET, false)) {
views.setViewVisibility(R.id.widget_root, View.GONE);
}
}
// Get Entry from current playing DownloadFile
MusicDirectory.Entry currentPlaying = null;
if(service == null) {
// Deserialize from playling list to setup
try {
PlayerQueue state = FileUtil.deserialize(context, DownloadServiceLifecycleSupport.FILENAME_DOWNLOADS_SER, PlayerQueue.class);
if (state != null && state.currentPlayingIndex != -1) {
currentPlaying = state.songs.get(state.currentPlayingIndex);
}
} catch(Exception e) {
Log.e(TAG, "Failed to grab current playing", e);
}
} else {
currentPlaying = service.getCurrentPlaying() == null ? null : service.getCurrentPlaying().getSong();
}
String title = currentPlaying == null ? null : currentPlaying.getTitle();
CharSequence artist = currentPlaying == null ? null : currentPlaying.getArtist();
CharSequence album = currentPlaying == null ? null : currentPlaying.getAlbum();
CharSequence errorState = null;
// Show error message?
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_SHARED) ||
status.equals(Environment.MEDIA_UNMOUNTED)) {
errorState = res.getText(R.string.widget_sdcard_busy);
} else if (status.equals(Environment.MEDIA_REMOVED)) {
errorState = res.getText(R.string.widget_sdcard_missing);
} else if (currentPlaying == null) {
errorState = res.getText(R.string.widget_initial_text);
}
setText(views, title, artist, album, errorState);
// Set correct drawable for pause state
if (playing) {
views.setImageViewResource(R.id.control_play, R.drawable.media_pause_dark);
} else {
views.setImageViewResource(R.id.control_play, R.drawable.media_start_dark);
}
setImage(views, context, currentPlaying);
// Link actions buttons to intents
linkButtons(context, views, currentPlaying != null);
pushUpdate(context, appWidgetIds, views);
}
/**
* Return the currently set summary. If {@link #summaryRes} is set,
* this resource is loaded from <var>res</var> and returned. Otherwise
* {@link #summary} is returned.
*/
public CharSequence getSummary(Resources res) {
return summaryRes != 0 ? res.getText(summaryRes) : summary;
}