Perform a search on google chrome using ADB - android

I'm setting up a python script for doing some tests on android phones.
I can actually launch a new chrome tab on my phone with ADB:
shell am start -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity -d "about:newtab" --activity-clear-task
I can't figure out the way I could perform a search on chrome with the search provider included in.

The suggested command by #oss does not care about user settings/defaults , it only require that google chrome is installed!
You may need to create a function like this:
function adbgs () {
local str="${*}";
adb shell am start -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity -d "google.com/search?q=${str// /+}" --activity-clear-task
}
adbgs how to do stuff;
Or if you want search using default browser :
function adburlopen () {
adb shell am start -a android.intent.action.VIEW -d "${1}"
}
# google
function adbgs () {
local str="${*}";
adburlopen "https://www.google.com/search?q=${str// /+}";
}
# yahoo
function adbys () {
local str="${*}";
adburlopen "https://sg.search.yahoo.com/search?q=${str// /+}";
}

Related

Can we open a browser with passing URL in golang android OS?

Working on a Go project and using gomobile to generate .apk file.
I am trying to open a browser in the go code with a passing URL.
I know go supports running CMD commands in different operating systems such as windows and Linux.
Wonder to know if there is any code to support Android OS as well.
In other words in following code what should I have under
case "android":
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
You can try using ADB Shell. Check this question Need command line to start web browser using adb
adb shell am start -a android.intent.action.VIEW -d 'http://stackoverflow.com/?uid=isme\&debug=true'

Is it possible that ADB communicate with 3rd party Android AIDL?

Android can use adb shell service call SERVICE to communicate with .aidl file, pass arguments and get return value. However, it belongs Android source code. Is it possible I can do the same thing after install non-system app which is written by myself?
For example, I write an app which its package name is com.test.aidl and there is a function inside.
fun add(x: Int, y: Int): Int {
return x +y
}
Can I use adb command, such as adb shell service call com.test.aidl add i32 1 i32 2 and get value 3 on command prompt?
I've resolved this using instrument.
ADB command
adb shell am insrument -e <NAME> <VALUE> <package/runner>
Android
public class Test extends android.test.InstrumentationTestRunner{
#Override
public void onCreate(Bundle bundle) {
super.onCreate(arguments);
value = (String) bundle.get("name");
}
Then use sendStatus api to print on CMD.

Enable/Disable wifi using Xamarin UiTest

I try to enable/disable wifi programmatically within my Xamarin Ui Test.
I already found this: Android: How to Enable/Disable Wifi or Internet Connection Programmatically. But it seems not to work within the UiTest.
I also tried something like this:
Context appContext = Android.App.Application.Context;
var wifiManager = (WifiManager)appContext.GetSystemService(Context.WifiService);
bool status = false;
wifiManager.SetWifiEnabled(status);
The first line (Android.App.Application.Context) throws an exception:
Message: System.IO.FileNotFoundException : Could not load file or assembly 'Java.Interop, Version=0.1.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' or one of its dependencies. The system cannot find the file specified.
I'm using following namespaces:
using Android.Net.Wifi;
using Android.Content;
My project has a reference to Mono.Android.
The backdoor approach works fine for me.
The solution that works for me was a combination of:
Android: How to Enable/Disable Wifi or Internet Connection Programmatically
https://learn.microsoft.com/en-us/appcenter/test-cloud/uitest/working-with-backdoors
Some small own changes (for appcontext/context). Just context did not work for me.
1.: Add following line to the AndroidManifest.xml file of the Android project:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
2.: Add following lines to the MainActivity.cs of the Android project:
using Java.Interop;
using Android.Net.Wifi;
[Export("ChangeWifiState")]
public void ChangeWifiState(bool state)
{
Context appContext = Android.App.Application.Context;
var wifiManager = (WifiManager)appContext.GetSystemService(WifiService);
wifiManager.SetWifiEnabled(state);
}
3.: Call following method out of the Xamarin Ui Test:
app.Invoke("ChangeWifiState", false); // true to enable wifi, false to disable wifi
PS: I use Xamarin Forms. I've got four different projects: a core project, an Android project, a Ui project, and a test project.
I just found a second solution without using the actual app.
It uses ADB commands to enable/disable wifi:
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 23 & adb shell input keyevent 82 & adb shell input tap 500 1000"
};
process.StartInfo = startInfo;
process.Start();
This can be used without a rooted device :).
Steps explained:
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings opens the wifi settings.
adb shell input keyevent 23 enables/disables wifi.
I'm not sure why the command adb shell input keyevent 19 is used, but it works.
adb shell input keyevent 82 clicks the menu button to change back to the original app.
adb shell input tap 500 1000 clicks the coordinate x=500, y=1000 (center of screen).This may need be changed for different solutions.
Sources for this solution:
How to turn off Wifi via ADB?
ADB Shell Input Events
Run Command Prompt Commands

Doze whitelist and deviceidle.xml

I'm trying to add my package in doze whitelist.
With ($ adb shell dumpsys deviceidle whitelist +PACKAGE), I can add my package in whitelist,
and this command makes change in the file /data/system/deviceidle.xml.
Now, I'm curious about who generate deviceidle.xml.
Is there anyone who knows about deviceidle.xml?
I found a clue in framework module,
IDeviceController.addPowerSaveWhitelistApp(String name) helps to add my package into ;
also, /data/system/deviceidle.xml is updated.
You can check with adb dumpsys
$ adb shell dumpsys deviceidle
$ adb shell cat /data/system/deviceidle.xml
As far as I know, doze whitelisting is done by system creators.
You can ask user for whitelisting via intent action: Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS.
In the DeviceIdleController soruce code you can see that it reads deviceidle.xml
in constructor. It might be that, if you know the file structure and have rooted device you can manually create and edit this file.
#VisibleForTesting DeviceIdleController(Context context, Injector injector) {
super(context);
mInjector = injector;
mConfigFile = new AtomicFile(new File(getSystemDir(), "deviceidle.xml"));
[...]
}

Create logcat filters for all applications

I have few standard filters that I have created for my app's logs. I use same format of logs for all of my apps. I want these filters to be available in every project I create with android studio e.g. Firebase filter available for all projects.
Is it possible to create these filter once and use them in all of my apps?
PS:
I have created a bash function which creates a logcat filter for any project by picking the project ID from build.gradle file.
I don't like it as I need to open separate console for it.
I just want filters in Android Studio.
function filterLog(){
TAG=`grep applicationId < app/build.gradle | sed 's/.*applicationId.*\.//' | tr -d '"'`
printf "filtering tag = %s\n" $TAG
adb shell logcat -c && adb shell logcat -v raw $TAG:D *:S
} # filterLog

Categories

Resources