Trouble using reflection with setOverScrollMode - android

I want to be able to turn off overscroll (the glowing effect when reaching the top or bottom of a page in 2.3+) however I also want my code to run in older versions of android that don't even have overscroll functionality. As per the documentation here: Android Backwards Compatibility I am using reflection in my custom webview class to call setOverScrollMode however everytime I call this on a device running 2.3.4, I get a NoSuchMethodException. Any idea why I can't retrieve this method?
Strangely, if I just call setOverScrollMode without any reflection, it works, so the method is definitely there.
public class MyWebView extends WebView{
public void compatibilitySetOverScroll(){
try {
Method mWebview_SetOverScroll = WebView.class.getMethod("setOverScrollMode", new Class[] { Integer.class } );
/* success, this is a 2.3+ */
if (mWebview_SetOverScroll != null) {
try {
mWebview_SetOverScroll.invoke(this, 2);
} catch (InvocationTargetException ite) {
throw new RuntimeException(ite.getCause());
} catch (IllegalAccessException ie) {
System.err.println("unexpected " + ie);
}
}
} catch (NoSuchMethodException nsme) {
/* failure, must be older device */
}
}
}

Try Integer.TYPE instead of Integer.class

More correct version:
public static void disableOverscroll(View view) {
Class<?> viewCls = view.getClass();
try {
Method m = viewCls.getMethod("setOverScrollMode",
new Class[] { int.class });
int OVER_SCROLL_NEVER = (Integer) viewCls.getField(
"OVER_SCROLL_NEVER").get(view);
m.invoke(view, OVER_SCROLL_NEVER);
} catch (Exception e) {
// swallow
}
}

another way :
try
{
Class<?> myTarget = Class.forName("android.widget.HorizontalScrollView");
Method myMethod = myTarget.getDeclaredMethod("setOverScrollMode", Integer.TYPE);
myMethod.invoke(scrollView, 2);
}
catch (Exception e)
{
e.printStackTrace();
}

Related

How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)

I know how to turn on/off wifi hot spot using reflection in android using below method.
private static boolean changeWifiHotspotState(Context context,boolean enable) {
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Method method = manager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class,
Boolean.TYPE);
method.setAccessible(true);
WifiConfiguration configuration = enable ? getWifiApConfiguration(manager) : null;
boolean isSuccess = (Boolean) method.invoke(manager, configuration, enable);
return isSuccess;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
But the above method is not working Android 8.0(Oreo).
When I execute above method in Android 8.0, I am getting below statement in logcat.
com.gck.dummy W/WifiManager: com.gck.dummy attempted call to setWifiApEnabled: enabled = true
Is there any other way to on/off hotspot on android 8.0
I thought the LocalOnlyHotspot route was the way to, but as #edsappfactory.com said in the comments - it only gives closed network, no internet access.
In Oreo hot-spotting/tethering moved to ConnectionManager, and its annotated #SystemApi, so (nominally) inaccessible.
As part of something else I was doing, I made an app and put it on github here. It uses reflection to get at the function and DexMaker to generate a subclass of ConnectionManager.OnStartTetheringCallback (which is also inaccessible).
Think it all works okay - bit rough around the edges, so please feel free to make better!
Relevant bits of code are in:
MyOreoWifiManager and;
CallbackMaker
I lost patience trying to get my DexMaker-generated callback to fire the MyOnStartTetheringCallback so all that code is in disarray and commented out.
Finally I got the solution.
Android 8.0, they provided public api to turn on/off hotspot. WifiManager
Below is the code to turn on hotspot
private WifiManager.LocalOnlyHotspotReservation mReservation;
#RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot() {
WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
#Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
Log.d(TAG, "Wifi Hotspot is on now");
mReservation = reservation;
}
#Override
public void onStopped() {
super.onStopped();
Log.d(TAG, "onStopped: ");
}
#Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.d(TAG, "onFailed: ");
}
}, new Handler());
}
private void turnOffHotspot() {
if (mReservation != null) {
mReservation.close();
}
}
onStarted(WifiManager.LocalOnlyHotspotReservation reservation) method will be called if hotspot is turned on.. Using WifiManager.LocalOnlyHotspotReservation reference you call close() method to turn off hotspot.
Note:
To turn on hotspot, the Location(GPS) should be enabled in the device. Otherwise, it will throw SecurityException
As per Jon suggestion, I got another way to enable WifiHotSpot in Android Oreo and above.
public boolean enableTetheringNew(MyTetheringCallback callback) {
File outputDir = mContext.getCodeCacheDir();
try {
proxy = ProxyBuilder.forClass(classOnStartTetheringCallback())
.dexCache(outputDir).handler(new InvocationHandler() {
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "onTetheringStarted":
callback.onTetheringStarted();
break;
case "onTetheringFailed":
callback.onTetheringFailed();
break;
default:
ProxyBuilder.callSuper(proxy, method, args);
}
return null;
}
}).build();
} catch (IOException e) {
e.printStackTrace();
}
ConnectivityManager manager = (ConnectivityManager) mContext.getApplicationContext().getSystemService(ConnectivityManager.class);
Method method = null;
try {
method = manager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, classOnStartTetheringCallback(), Handler.class);
if (method == null) {
Log.e(TAG, "startTetheringMethod is null");
} else {
method.invoke(manager, TETHERING_WIFI, false, proxy, null);
}
return true;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
private Class classOnStartTetheringCallback() {
try {
return Class.forName("android.net.ConnectivityManager$OnStartTetheringCallback");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}

How to get Advertising id in android?

I used this code it's working when i called from activity and fragment
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesAvailabilityException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
String AdId = adInfo.getId();
But when i called from pending intent like Package Removed then i want to call the web service at that time i need advertising id but i got null.if you people had done previously please suggest me.thanks in advance.
try use app context
as receivers could use activity context or app context - depends who&how started the receiver
AdvertisingIdClient.getAdvertisingIdInfo(context.getApplicationContext());
You can try this code and call the bellow method on onCreate
public void getAAID()
{
AsyncTask.execute(new Runnable() {
#Override
public void run() {
try {
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MyActivity.this);
String myId = adInfo != null ? adInfo.getId() : null;
Log.i("UIDMY",myId);
} catch (Exception e) {
Log.e("error", e);
}
}
});
}
Check complete post: how to get AAID programmatically

Is it possible to test a RoboContentProvider?

I've been struggling around with this test. I want to test my created ContentProvider, using RoboGuice.
Currently the ContentProvider I have extends from RoboContentProvider.
However, I'm not being able to Make It Run.
It says there's no implementation for an Interface. However, the modules are being defined and so one.
Here's the code for the setUp Method on the AndroidTestCase.
#Override
public void setUp() {
Context context = InstrumentationRegistry.getTargetContext();
Application app = null;
try {
app = InstrumentationRegistry
.getInstrumentation()
.newApplication(Application.class.getClassLoader(), Application.class.getName(), context);
RoboGuice.setUseAnnotationDatabases(false);
RoboGuice.overrideApplicationInjector(app,
RoboGuice.newDefaultRoboModule(app),
new DatabaseModule(app)
);
mContentResolver = new MockContentResolver();
mContentProvider = new MobiModelsContentProvider();
mContentProvider.attachInfo(app, null);
mContentResolver.addProvider("com.authority.models.test", mContentProvider);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

Crosswalk 13: Set Cache Mode

I've recently migrated from android webview to Crosswalk 13. The only issue i've run into is telling the XWalkView to load content from the app cache.
In my android webview implementation i had implmemented as this
//check connection on a loop
public void CheckConnectivityTask(){
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
//runs every 0.5s
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
return null;
}
#Override
protected void onPostExecute(Void result) {
CheckConnectivity(true);
}
}.execute();
}
public void CheckConnectivity(boolean recursiveTask){
cm = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
if(cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()){
Log.v("ConnectivityGG", "IS CONNECTED");
mainWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
}
else{
mainWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
if(recursiveTask){
CheckConnectivityTask();
}
}
As getSettings() has now been removed from XWalk 13, I've been trying to set this using XWalkSettings
inside OnCreate in MainActivity
xWalkSettings = new XWalkSettings(mainWebView.getContext(), null , false);
xWalkSettings.setAppCacheEnabled(true);
and then modifying my looped task
public void CheckConnectivity(boolean recursiveTask){
cm = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
if(cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()){
xWalkSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
}
else{
xWalkSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
if(recursiveTask){
CheckConnectivityTask();
}
}
However any attempt to load cached pages fails with "Internet connection has been lost" alert dialogue. Am I instantiating the XWalkSettings instance incorrectly, or is there another way of achieving this?
I found a way from this link. And changed it slightly. Basically need to use reflection to get access to a non public (afaik) method.
Method ___getBridge;
try {
___getBridge = XWalkView.class.getDeclaredMethod("getBridge");
___getBridge.setAccessible(true);
XWalkViewBridge xWalkViewBridge = null;
xWalkViewBridge = (XWalkViewBridge)___getBridge.invoke(mainWebView);
xWalkSettings = xWalkViewBridge.getSettings();
xWalkSettings.setAppCacheEnabled(true);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
If there's a nicer, cleaner way of doing this I'd love to know :)
Crosswalk didn't expose setCacheMode API before, but it has been exposed recently, please see this JIRA, https://crosswalk-project.org/jira/browse/XWALK-6832
It should be available in Crosswalk 21, you can use it like below:
mXWalkView.getSettings().setCacheMode(XWalkSettings.LOAD_NO_CACHE);
So, enjoy it.. :)

How to retrieve the 'last sync' time for an account?

Is it possible to retrieve the time an account was last synchronized, like the system Settings->Accounts&Sync app does? I'm using Android 2.2.
Looking at the 2.2 source for AccountSyncSettings.java, I see the status is retrieved using:
SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority);
but SyncStatusInfo and getSyncStatus don't seem to be part of the public API (marked with #hide). Is there some other way to get at this info?
You can use reflection to achieve this purpose.Here is my code to implement this
private long getLasySyncTime() {
long result = 0;
try {
Method getSyncStatus = ContentResolver.class.getMethod(
"getSyncStatus", Account.class, String.class);
if (mAccount != null && mSyncAdapter != null) {
Object status = getSyncStatus.invoke(null, mAccount,
mSyncAdapter.authority);
Class<?> statusClass = Class
.forName("android.content.SyncStatusInfo");
boolean isStatusObject = statusClass.isInstance(status);
if (isStatusObject) {
Field successTime = statusClass.getField("lastSuccessTime");
result = successTime.getLong(status);
TLog.d(WeixinSetting.class, "get last sync time %d", result);
}
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
TLog.d(WeixinSetting.class, e.getMessage() + e.getCause().getMessage());
} catch (IllegalArgumentException e) {
} catch (ClassNotFoundException e) {
} catch (NoSuchFieldException e) {
} catch (NullPointerException e) {
}
return result;
}
The Settings app uses ContentResolver.getSyncStatus(account, authority). However, this is not part of the public API. You can use it, but it could break with any future release.

Categories

Resources