I have this code in the ActionBarSherlock
#Override
public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event)
{
if (super.onRequestSendAccessibilityEvent(child, event)) {
// Add a record for ourselves as well.
AccessibilityEvent record = AccessibilityEvent.obtain();
onInitializeAccessibilityEvent(record);
// Populate with the text of the requesting child.
child.dispatchPopulateAccessibilityEvent(record);
event.appendRecord(record);
return true;
}
return false;
}
#Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setScrollable(isScrollableForAccessibility());
View selectedView = getSelectedView();
if (selectedView != null) {
info.setEnabled(selectedView.isEnabled());
}
}
#Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setScrollable(isScrollableForAccessibility());
View selectedView = getSelectedView();
if (selectedView != null) {
event.setEnabled(selectedView.isEnabled());
}
event.setCurrentItemIndex(getSelectedItemPosition());
event.setFromIndex(getFirstVisiblePosition());
event.setToIndex(getLastVisiblePosition());
event.setItemCount(getCount());
}
And I get compile errors on super.onInitializeAccessibilityEvent(event); and a few other methods.
Would anyone know why this happens?
Thanks!
The complaint is that your android:minSdkVersion is set to 8, and that code is referring to classes, methods, or fields that were not added until API Level 14.
If you are attempting to modify ActionBarSherlock and compile it yourself, you can either add the appropriate code to deal with this (checking Build.VERSION.SDK_INT) and add the #TargetApi() annotation to stop Lint from complaining.
Related
I am trying to turn on hardware acceleration for my application
but I never seem to get a 'true' result from this function.
I tried all the methods in the Android Developers blog post about the
the tag android:hardwareAccelerated=true to the application
and even called
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
You should always call isHardwareAccelearted() after the view is attached to the window. I am not sure about your case as I can't see where actually you are calling it.
Also, the support level of various operations across API levels are mentioned here. I hope this helps.
You can also try this method to verify hardwareAcceleration.
public static boolean hasHardwareAcceleration(Activity activity) {
// Has HW acceleration been enabled manually in the current window?
Window window = activity.getWindow();
if (window != null) {
if ((window.getAttributes().flags
& WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
}
// Has HW acceleration been enabled in the manifest?
try {
ActivityInfo info = activity.getPackageManager().getActivityInfo(
activity.getComponentName(), 0);
if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Chrome", "getActivityInfo(self) should not fail");
}
return false;
}
For checking for View try this.
chat_wv.post(new Runnable() {
#Override
public void run() {
Log.e("isHardwareAccelerated", ""+chat_wv.isHardwareAccelerated());
}
});
I have toggle(Switch) buttons inside my fragment.After coming on the fragment I am reading BLE values and setting the toggle buttons.
#Override
public void sosStatus(boolean sosvalue, BluetoothGattCharacteristic sosCharac) {
if (sosvalue) {
byte[] charValue = sosCharac.getValue();
String valueOfCharInstring = StringUtils.byteToHex(charValue[0]);
Log.d("+++++SosStatus",""+sosCharac.getUuid().toString() + " " + valueOfCharInstring);
if (sosCharac.getUuid().toString().equalsIgnoreCase(BLEConstants._BUTTON_CHARACTERISTIC)) {
if (valueOfCharInstring.equalsIgnoreCase(BLEConstants.EnableCharacInString)) {
setButtonStatus(touchButton,R.id.switch_btn_device_touch,"Enabled");
// touchButton.setChecked(true);
// tvTouchButtonAction.setText("Enabled");
} else if (valueOfCharInstring.equalsIgnoreCase(BLEConstants.DisableCharacInString)) {
setButtonStatus(touchButton,R.id.switch_btn_device_touch,"Disabled");
// touchButton.setChecked(false);
// tvTouchButtonAction.setText("Disabled");
}
}
if (characList.size() > 0) {
gattclientCallBack.readCharacteristicMain(UUID.fromString(characList.remove(characList.size() - 1)));
} else {
useOnCheckedChangeMethod = true;
showProgress(false);
}
} else {
useOnCheckedChangeMethod = true;
showProgress(false);
HandleCharacListData(true,false,"");
}
}
Now since Switch widget is used, what is happening is that when I read the values programatically for first time, it works fine.but when I toggle the button with touch, onCheckChanged is repeatedly getting called as if I set some value, it keeps on calling itself in infinite loop. This is my oncheckchanged code.
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
try {
if (useOnCheckedChangeMethod) {
switch (compoundButton.getId()) {
case R.id.switch_btn_device_touch:
touchButton.setOnCheckedChangeListener(null);
//showProgress(true);
HandleCharacListData(true,false,"");
HandleCharacListData(false,false,BLEConstants.TOUCH_BUTTON_CHARACTERISTIC);
if(characList!=null && characList.size()>0) {
if(b) {
gattclientCallBack.writeCharacteristic(characList.remove(characList.size() - 1), BLEConstants.DisableCharac);
}
else {
gattclientCallBack.writeCharacteristic(characList.remove(characList.size() - 1), BLEConstants.EnableCharac);
}
}
Log.d("Touch++++", "+++");
break;
}
So it continuously keep on toggling as on and off due to the check if(b). :)
what can I do to ensure that the onCheckChange methos only gets called once after the value is set ?
Things that I have also tried
1) Use onClick listener and disable call in oncheckchanged and enable on click.
2) Use onTouch
Thank you :)
That interesting, because inside of setChecked() it actually checks to see if it's in the middle of broadcasting and returns...
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
notifyViewAccessibilityStateChangedIfNeeded(
AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
// Avoid infinite recursions if setChecked() is called from a listener
if (mBroadcasting) {
return;
}
mBroadcasting = true;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
if (mOnCheckedChangeWidgetListener != null) {
mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
}
mBroadcasting = false;
}
}
The only solution I know of is un-registering the callback before calling setChecked() and register the callback again after your call returns. This works because the callback isn't called asynchronously but instead, called immediately inside of setChecked().
Hey I got my answer in the link below to a question framed little differently . Thanks to this guy :)
onCheckedChanged called automatically
In my first ever Android project I see a lot of code like this:
final boolean lNewButtonState = SOME_CONDITION;
if (lNewButtonState != mButtonState) {
mButtonState = lNewButtonState;
mButton.setEnabled(mButtonState);
}
Does it make any difference to set the state of an ImageButton (the type of mButton) only if it does change as opposed to setting it always? The button is visible on the screen when the code is executed.
I'd prefer a straightforward (and more readable):
mButton.setEnabled(SOME_CONDITION);
The question is, would it impose any drawbacks? Does the answer depend on the Android version (ours is Jelly Bean)?
Doesn't matter that much because it will be ignored if the button is already enabled. The only cost of it is the isEnabled() method call which is pretty insignificant
From Android source code:
#Override
public void setEnabled(boolean enabled) {
if (enabled == isEnabled()) {
return;
}
if (!enabled) {
// Hide the soft input if the currently active TextView is disabled
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null && imm.isActive(this)) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
super.setEnabled(enabled);
if (enabled) {
// Make sure IME is updated with current editor info.
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) imm.restartInput(this);
}
// Will change text color
if (mEditor != null) {
mEditor.invalidateTextDisplayList();
mEditor.prepareCursorControllers();
// start or stop the cursor blinking as appropriate
mEditor.makeBlink();
}
}
#ViewDebug.ExportedProperty
public boolean isEnabled() {
return (mViewFlags & ENABLED_MASK) == ENABLED;
}
I have a this code somewhere in my Android project:
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
I get a lint warning at the second if statement: 'if' statement could be simplified. That's obviously because I could write as well:
return publicLoad && publicLoadInProgress;
However, I would like to keep it this way for readability. I know that there is some inline comment annotation for switching off the lint warning at that place, but I can't find it in the Android Lint documentation. Can you tell me what this annotation/comment was?
The simple code comment for disabling the warning is:
//noinspection SimplifiableIfStatement
This on top of the if-statement should switch off the warning only at that place.
In the example, this would be:
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
//noinspection SimplifiableIfStatement
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
You can add #SuppressWarnings("SimplifiableIfStatement") above your method.
It's not an Android Lint error. You can use:
#SuppressWarnings("RedundantIfStatement")
public static boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
At the highlighted if, you can use the alt-enter shortcut to open the context menu and select Simplify > Suppress for method (keeping the scope as small as possible).
Sure:
In .java files, you can suppress issues with the #SuppressLint
annotations. You supply the lint issue id as the argument to the
annotations.
Example:
#SuppressLint("AndroidWarningId")
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress) {
return true;
}
if (publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
Just replace the AndroidWarningId with the corresponding warning, you can find those in here
Although I would suggest simplifying it this way:
public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
if (privateLoad && privateLoadInProgress
|| publicLoad && publicLoadInProgress) {
return true;
}
return false;
}
Its still readable and uses less space (kind of ugly though, but better than a supresslint).
You can also suppress more than one issue using a comma separated list:
#SuppressLint({"NewApi","StringFormatInvalid"})
Cheers!
I am use Otto library in my project. And me need any functionality from this library wherein not.I want to do so:
Bus.post(MessageType.USER_SIGN_UP_SUCCESS, user);
and in my method realise do so:
#Subscribe({MessageType.USER_LOGGED_IN_SUCCESS, MessageType.USER_SIGN_UP_SUCCESS})
public void getUserFromServer(User user) {
,,,,,,,,,,,,,,
}
for this I had to copy all Otto classes from githab, and change them. I could not implement from Otto because some variables private.
and changed the access modifiers in Bus class and extends from it.
public class SkipBus extends Bus {
public void post(MessageType messageType, Object event) {
if (event == null) {
throw new NullPointerException("Event to post must not be null.");
}
enforcer.enforce(this);
Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());
boolean dispatched = false;
for (Class<?> eventType : dispatchTypes) {
Set<EventHandler> wrappers = getHandlersForEventType(eventType);
if (null == wrappers || wrappers.isEmpty()) {
continue;
}
dispatched = true;
for (EventHandler wrapper : wrappers) {
Subscribe annotation = wrapper.method.getAnnotation(Subscribe.class);
boolean isFounded = false;
MessageType messageTypes[] = annotation.value();
for (MessageType type : messageTypes) {
if (type == messageType) {
isFounded = true;
break;
}
}
if (isFounded) {
enqueueEvent(event, wrapper);
}
}
}
if (!dispatched && !(event instanceof DeadEvent)) {
post(new DeadEvent(this, event));
}
dispatchQueuedEvents();
}
}
but for this I had to copy all the classes in my project.
tell me how can I make it easier? or tell me another library that can do what I want
Actually Otto was designed to separate the message type using Object's type.
#Subscribe
public void eventReceived(UserSignUpEvent user) {
}
#Subscribe
public void eventReceived(UserLoginEvent user) {
}
or
MainActivity.java
#Subscribe
public void eventReceived(User user) {
if (user.getMessageType() == MessageType.USER_SIGN_UP_SUCCESS) {
}
}
SecondActivity.java
#Subscribe
public void eventReceived(User user) {
if (user.getMessageType() == MessageType.USER_LOGIN_SUCCESS) {
}
}
You have no need to separate the MessageType like this (and you should not). I suggest you to change the code design pattern to what that Otto is designed for. Otherwise, you have to copy the whole Otto source code and edit like you are currently doing.
Otto itself is pretty simple so there's no need to extend it. You just create event and event handler for it. Event can be any object thus you can do something like:
public enum LoginType {
SUCCESS,
FAILED
}
In your login processor:
private void login(...) {
// process login
// if everything is ok
if(...) {
bus.post(LoginType.SUCCESS);
} else {
bus.post(LoginType.FAILED);
}
}
And handle this event wherever you need:
#Subscribe
public void onLogin(LoginType loginType) {
switch(loginType) {
case SUCCESS:
// do what you need
break;
case FAILED:
// show an error or something
break;
}
}
So no need to extend library, you just have to design your events in a proper way.