Android and PyQt application - android

I wrote an application using PyQt for Windwos/Linux and I want to port it to Android. Application is simple, has 2 levels of GUI and I do not want to implement touch functions, but to control application using USB keyboard.
I am using this project:
android-python27
to interpret my PyQt code for Android, but it doesn't work. I suppose that I have to modify code, but I do not know how. I only found one sample for using android-python27:
import android, time
droid = android.Android()
while 1:
droid.makeToast("Hello from Python 2.7 for Android")
time.sleep(5)
My application is being started in this way:
app = QtGui.QApplication(sys.argv)
app.Encoding(QtGui.QApplication.UnicodeUTF8)
ui = MainMenu()
class MainMenu(QtGui.QWidget):#glavni izbornik
def __init__(self):
super(MainMenu, self).__init__()
i=0
self.UI(self)
def UI(self, Form):
...
What should I change to adopt code to android-python27?

Related

Is it possible to do Android development using the R programming language?

I'm doing digital signal processing work in R using R Studio and think it would be a neat idea to expand this into an Android app. I know people have used Kivy to develop Android Apps in Python and I know it's possible to wrap R code in Python, so is this feasible? I read that it's possible to run R code on Android, just not sure if adding it to my project is a possibility. I also don't need to have the entire app to be written in R. Are there any examples/documentation related to this?
Turns out you can! I've built a more complex app using Kivy and the rpy2 package, but I'll demonstrate a simple example here.
You can define your R code like so:
r_code.py
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage
r_code = """
add <- function(x, y) {
return(x + y)
}
"""
r_lib = SignatureTranslatedAnonymousPackage(r_code, "r_lib")
Your Kivy App will look something like this:
main.py
from kivy.app import App
from r_code import r_lib
class MainApp(App):
def build(self):
window = BoxLayout(orientation='vertical')
label = Label(text=str(r_lib.add(3, 4)[0]))
b.add_widget(label)
return b
if __name__ == "__main__":
MainApp().run()
And if you want to package this into an Android App, you can follow the directions from the official doc. If you run into an error with rpy2, you might have to install the correct version for your system from here.
Happy coding!

RAPT - android module's check_pause, accelerometer, etc. does not work

I know people have asked questions along these lines here, but I haven't found an answer that helps me.
I've made a simple game in pygame, I'm using RAPT to port it to android. I've gotten the game to compile and run successfully on my tablet. Great.
It goes something like this:
import android
android.init()
import pygame
pygame.init()
def main():
#does stuff for the game
The standard stuff.
But as soon as I add these lines below the import android:
enable = True
android.accelerometer_enable(enable)
or
android.check_pause()
or anything in the android module other than init(), the game refuses to run. ADB logcat shows that the code throws an AttributeError: 'module' object has no attribute of (whatever function I tried)
I've even tried copy-pasting code from https://github.com/codetricity/accel/blob/master/main.py
and trying to compile the example code given from the RAPT download page...
https://github.com/renpytom/rapt-pygame-example/blob/master/main.py
and it returns the same kind of errors once compiled.
What's going on?
In RAPT, accelerometer is seen as a 3-axis joystick. So you can use the pygame joystick module.
pygame.joystick.init()
num_joy_presenti = pygame.joystick.get_count()
accel = None
for i in range(num_joy_presenti):
generico_joy = pygame.joystick.Joystick(i)
generico_joy.init()
if generico_joy.get_name() == "Android Accelerometer":
accel = pygame.joystick.Joystick(i)
accel.init()
Then, "accel" will be your accelerometer and you can use the joystick module to get its values. Documentation here http://www.pygame.org/docs/ref/joystick.html#pygame.joystick.Joystick
Instead of android.check_pause you can detect pygame.APP_WILLENTERBACKGROUND and pygame.APP_WILLENTERFOREGROUND events as in the example at https://github.com/renpytom/rapt-pygame-example/blob/master/main.py

Function "on_complete" called before "completed" on kivy app with plyer

It's me again!
Well, that's really strange.
I'using kivy for make an App for Android.
I can use the camera, but or the app resets or do something strange.
Here is the problem:
def chamar_camera(nome,pc,objeto,label_passa,instance):
agora = datetime.now()
nome_arquivo = '%s_%s_%.4i_%.2i_%.2i_%.2i_%.2i_%.2i.jpg' % (nome,pc,agora.year,agora.month,agora.day,agora.hour,agora.minute,agora.second)
# Option 1 - These two lines work:
#def sair():print 'oi'
#camera.take_picture(nome_arquivo, sair)
# Option 2 - These two lines work too:
def sair(label_passa,nome_arquivo):print 'oi'
camera.take_picture(nome_arquivo,on_complete=sair(label_passa,nome_arquivo))
# Option 3 - But these don't:
#def sair(label_passa,nome_arquivo):label_passa.text = nome_arquivo
#camera.take_picture(nome_arquivo, on_complete=sair(label_passa,nome_arquivo))
def on_pause(self):return True
def on_resume(self):pass
On option 3, I write a text (nome_arquivo) on a label widget (label_passa), but what happens is that the text is wrote before the camera be activated. So the camera appear, I can take a picture and the App restarts. I also tried just a "def sair(): pass", but this doesn't work. The only thing working is an "print", but on my app I need to write something in that label and update an sqlite database. Any idea why the function is being called before the camera action?
Thanks!
on_pause and on_resume should be defined as methods of your App class, not (as you have here) functions defined locally within the chamar_camera function.

Generic mobile tests with appium

I'm new to mobile testing , and currently I research for an automation framework for mobile testing.
I've started to look into Appium, and created some tests for the demo app I've made (one for IOS and the other for Android).
I've managed to write a test for each of the platforms , but I was wondering , how difficult it might be to write a one generic test which will be executed on the both platforms with minimum adjustments ?
Thanks
It is possible but you have to keep same labels for each component for all platforms, for example to click on a button, instead of locating through Xpath locate by its name.
WebElement button = driver.findElement(By.name("my button")); button.click();
More info finding elements in Appium docs:
http://appium.wikia.com/wiki/Finding_Elements
I built an automation framework from scratch which does exactly the same thing, i.e. have one code base and the tests run both on Android and iOS based on what device and app you give the test. This is how I went about doing it. (I used Java+Appium+Cucumber framework).
Following the Page Object pattern is a good practice for writing automation code.
That being said, you will have all the resource ids of Android and Accessibility ids of iOS in 2 separate files under a folder named say "ObjectRepository". These files usually have the extension of *.properties (It is called the properties file).
Say you have a Login button that you want to interact with on Android and iOS, you have will 2 files:
File 1) "androidObject.properties" which has:
Login.LoginButton=loginAndroidBtn
File 2) "iOSObject.properties" which has:
Login.LoginButton=loginiOSBtn
NOTE: In the key/value pair above, the key is the same "Login.LoginButton", the value is the resource id and the accessibility id of the Login Button in your Android and iOS application
In your code you would do the following:
if(IS_ANDROID) {
DRIVER.findElementById("Login.LoginButton").click();
} else {
DRIVER.findElementByAccessibilityId("Login.LoginButton").click();
}
In another file you would set what IS_ANDROID and IS_IOS means. You may do something like this:
public static DeviceConfig DEVICE_CONFIG;
private void setPlatform() {
if (DEVICE_CONFIG.platformName.equals("Android")) {
IS_ANDROID = true;
} else if (DEVICE_CONFIG.platformName.equals("iOS")) {
IS_IOS = true;
}
This way you can have one code base and run Android and iOS seamlessly.

Same Titanium code base for Android & Iphone

I have been trying to create a single codebase for both Iphone & Android for a intermediate level app. ( 4 tabs, multiple windows, maps etc.) using itanium 2.1 API.
However, I have found that things on Android platform dont work as smoothly or willingly as on Iphone epsecially tableviews & UI elemnts. The UI responsiveness on Android is also sluggish.
The kitchen sink examples are pretty straightforward. I am looking at an enterprise ready app which has to be maintained for atleast next couple of years.
Has anybody worked on similar lines with platform quirks and been successful in creating fully functional iOS & Android apps from SAME codebase?
I'm having a lot of success using the compile-time CommonJS mechanism for having a root view that then has os-specific capabilities.
For instance, my os-independent view might be ui/MyView.js :
var createAddButton = require("ui/MyView.AddButton");
var MyView = function() {
var self = Ti.UI.createWindow();
createAddButton(self, function() { alert('ADD!'); });
return self;
};
module.exports = MyView;
Then, I create os-specific functions to handle it:
iphone/ui/MyView.AddButton.js
module.exports = function(view, addHandler) {
var addButton = Titanium.UI.createButton({
systemButton: Titanium.UI.iPhone.SystemButton.ADD
});
addButton.addEventListener("click", addHandler);
view.rightNavButton = addButton;
};
android/ui/MyView.AddButton.js
module.exports = function(view, addHandler) {
view.activity.onCreateOptionsMenu = function(e){
var menuItem = e.menu.add({ title: "Add" });
menuItem.addEventListener("click", addHandler);
};
};
The CommonJS system they have implemented will pick the appropriate version of MyView.AddButton.js so that the button is added to the right place. It allows for the majority of the view to be the same, but the os-specific things to be separated properly.
Titanium is not meant for 1 codebase for all. You do need to rewrite stuff for every OS. However, some app developers claim to have reused 95% of its code. So only 5% of the code is OS specific. But I am sure their code is full with if-elses.
What I recommend doing, to be able to maintain it properly, without thousands of if-else constructions, is build a single backend core, and write code specifically for UI related matters per OS. This way, you have some UI related code for Android, UI related code for iOS and 1 core working for both.
Since Android and iOS differ a lot, writing a single codebase will make sure you can never use OS specific features (like android hardware menu button, or iOS NavigationGroup), and will let the UI look non-intuitive.

Categories

Resources