import widget in kivy file - android

I created some custom widget.
from kivy.uix.widget import Widget
from kivy.lang import Builder
class ExampleWidget(Widget):
Builder.load_file("kv/example.kv")
kv/example.kv
#:kivy 1.9.1
<ExampleWidget>:
Label:
text: Example
Than I want to create another widget with example widget. Like this:
kv/second.kv
#:kivy 1.9.1
<SecondWidget>:
ExampleWidget:
But I got this error
kivy.factory.FactoryException: Unknown class <ExampleWidget>
Kivy can't find my custom widget, so how I can import it to another kivy file?

You can import it using following syntax (assuming that ExampleWidget is defined in example.py file and you have __init__.py in your directory):
#: import ExampleWidget example.ExampleWidget
<SecondWidget>:
ExampleWidget:
Described in the documentation.

Related

Buildozer how to package app with service

I have a simple program to try making an app with a service that plays a song. Here is my code:
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
kv = '''
Button:
text: 'push me!'
'''
class ServiceApp(App):
def build(self):
if platform == 'android':
from android import AndroidService
service = AndroidService('Song App', 'Song Playing')
service.start('service started')
return Builder.load_string(kv)
ServiceApp().run()
and service main.py
from kivy.core.audio import SoundLoader
#I have a file called song.wav in the same directory
sound = SoundLoader.load('song.wav')
if sound:
sound.play()
This works perfectly in Kivy Launcher, but I don't know how I can package this with buildozer. Any help would be appreciated!

Easy kivy app work on windows but not on Android

I'm trying to study Kivy, so I've done a simple gui that work on windows but when I run it on Android with Kivy Launcher I'm getting this error trought ADB LOGCAT :
This is a simple kv code :
#:kivy 1.0.9
<ROT>:
FloatLayout:
AnchorLayout:
anchor_x:"center"
anchor_y: "top"
Button:
size_hint : 1,1
text : "prova"
on_press: root.press()
and this is the main.py :
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
class ROT(Widget):
def press(self):
print "ciao"
pass
class PongApp(App):
def build(self):
self.root = Builder.load_file('questionario.kv')
return ROT()
if __name__ == '__main__':
PongApp().run()
I can't understand why it doesn't want to work

Python Kivy - App that open url in a native web browser

I try to make a simple app that open a web page inside Kivy after clicking a button placed on a "Screen One".
I used this topic (Python - Showing a web browser/iframe right into the app) as reference but I didn't understand how to use the code provided by Michael...
So I tried this... and when I launch the apk (build with Buildozer) it didn't work :')
import kivy
kivy.require('1.9.2')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
# MICHAEL'S CODE
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
#run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com/')
# END OF MICHAEL'S CODE
Builder.load_string('''
<ScreenOne>:
BoxLayout:
Label:
text: "SCREEN 1"
Button:
text: "GO GO GO TO GOOGLE !"
on_press: root.open_browser()
<ScreenTwo>:
BoxLayout:
Label:
text: "SCREEN 2"
Button:
text: "GO GO GO TO SCREEN 1"
on_press:
root.manager.transition.direction = "right"
root.manager.transition.duration = 1
root.manager.current = "screen_one"
''')
class ScreenOne(Screen):
def open_browser(self):
return Wv()
class ScreenTwo(Screen):
pass
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
class BrowserApp(App):
def build(self):
return screen_manager
app = BrowserApp()
app.run()
The app don't crash but just close when I start it.
What I'm doing wrong ? I'm sure that I don't use it the right way...
Log from adb logcat:
06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
From the log you posted in the comments I extracted the two important lines:
06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
This basically means that the copied code:
from android.runnable import run_on_ui_thread
won't work because it doesn't detect the android module. The module has a separate recipe, therefore you will need to add it to the requirements, so that it compiles the Cython code and adds it to your app, otherwise the import will always fail.
Basically you always want to search for 3-4 keywords when looking into such a messy logcat → "python", "Traceback", "Python for android", "kivy". There's a filter in buildozer for that if you use it:
android.logcat_filters = *:S python:D

import android.preview.support.wearable.notifications.WearableNotifications;

I have a problem with "Stacking Notifications For Android".
So, It was show error when i try to import "import android.preview.support.wearable.notifications.WearableNotifications;"
How to import this class?
Thx for your support.
--
[D]
Looks like some wear class names and packages have changed from the preview to the full sdk:
Since the APIs and package names have changed, the import statements at the top of MainActivity.java need to be adjusted like this:
-import android.preview.support.v4.app.NotificationManagerCompat;
-import android.preview.support.wearable.notifications.WearableNotifications;
+import android.support.v4.app.NotificationCompat;
+import android.support.v4.app.NotificationManagerCompat;

custom fortify rule with android

I'd like to write a fortify rule that looks for instances of "addJavascriptInterface" in android codebases.
<?xml version="1.0" encoding="UTF-8"?>
<RulePack xmlns="xmlns://www.fortifysoftware.com/schema/rules">
<RulePackID>80B927D2-5408-41B4-B47C-B4958DAAECBD</RulePackID>
<SKU>SKU-80B927D2-5408-41B4-B47C-B4958DAAECBD</SKU>
<Name><![CDATA[android.xml]]></Name>
<Version>1.0</Version>
<Description><![CDATA[Description for android.xml]]></Description>
<Rules version="3.13">
<RuleDefinitions>
<SemanticRule formatVersion="3.13" language="java">
<MetaInfo>
<Group name="Accuracy">5.0</Group>
<Group name="Impact">5.0</Group>
<Group name="RemediationEffort">15.0</Group>
<Group name="Probability">5.0</Group>
</MetaInfo>
<RuleID>CC4B8F82-0824-4DF1-8A5F-513DC6820B99</RuleID>
<VulnCategory>Testjsinterface</VulnCategory>
<DefaultSeverity>5.0</DefaultSeverity>
<Description/>
<Type>default</Type>
<FunctionIdentifier>
<NamespaceName>
<Pattern>\w*</Pattern>
</NamespaceName>
<ClassName>
<Pattern>WebView</Pattern>
</ClassName>
<FunctionName>
<Pattern>addJavascriptInterface</Pattern>
</FunctionName>
<ApplyTo implements="true" overrides="true" extends="true"/>
</FunctionIdentifier>
</SemanticRule>
</RuleDefinitions>
</Rules>
</RulePack>
When I tried to run fortify against a codebase, I got the following :-
[warning]: The following references to java classes could not be resolved. Please make sure to supply all the required jar files that contain these classes to SCA.
Override
android.app.Activity
android.content.res.AssetManager
android.os.Bundle
android.util.Log
android.view.KeyEvent
android.webkit.WebSettings
android.webkit.WebView
android.webkit.WebViewClient
So i tried running fortify as follows :-
bin/sourceanalyzer ~/repos/android-rule-tests/WebViewTest -cp ~/android-sdks/platforms/android-16/android.jar
The warning goes away but the rule still does not seem to fire -- what could be going wrong?
EDIT
For clarity, WebViewTest is a sample project which uses addJavascriptInterface.
EDIT2
Added some code for clarity
package org.infil00p.phoneTest;
import java.io.IOException;
import com.infil00p.phoneTest.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.res.AssetManager;
public class TestActivity extends Activity {
WebView appView;
TestWebViewClient testClient;
String TAG="FAILTAG";
Bolt data = new Bolt();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testClient = new TestWebViewClient();
appView = (WebView) findViewById(R.id.appView);
appView.getSettings().setDatabaseEnabled(true);
appView.getSettings().setJavaScriptEnabled(true);
appView.addJavascriptInterface(data, "test");
appView.setWebViewClient(testClient);
appView.loadUrl("file:///android_asset/index.html");
}
....
EDIT3
I played around with this a bit and eventually reached the conclusion that in my particular case a semantic rule would not fire, until it saw a function thats defined in a class in the same file. Getting the android sources and copying over the source of WebView.java causes the rule to fire(a horrible thing to do but im just playing around).
Firstly I would advise the pattern for namespace to be .*. Secondly, where are you saving the custom rule file to? If not in <SCA install>/Core/config/customrules then it needs to be specified with the -rules option.
Also you are missing a build ID, your commands should be something like:
sourceanalyzer -b test -clean
sourceanalyzer -b test -source 1.6 ~/repos/android-rule-tests/WebViewTest -cp ~/android-sdks/platforms/android-16/android.jar -rules /path/to/rules/file.xml
sourceanalyzer -b test -scan -f myResults.fpr
I think with Android there's also an R class file created at runtime. Because SCA doesn't do this, the project should be built first so this can be specified in the classpath somewhere.
CLEAN CACHE: ~/sourceanalyzer -b test -clean
TRANSLATE (no need to mention -filter, -rules, -project-template during code translation): ~/sourceanalyzer -b test -source 1.6 ~/repos/android-rule-tests/WebViewTest -cp ~/android-sdks/platforms/android-16/android.jar
SCAN (where rules/filters/template being specified): ~/sourceanalyzer -b test -filter 'absolute_path_file.txt' -rules 'absolute_path_rulename.xml' -scan -f myResults.fpr
if you place the custom_rule.xml to the FORTIFY_HOME/Core/config/rules/ directory (along with the HP rulepacks), you can omit the -rules parameter during scan.

Categories

Resources