class GablesPython(static_proxy()): @constructor([]) def __init__(self): pass @method(jvoid, []) def processCPURoofline(self): try: os.chdir( os.path.join(str(Environment.getExternalStorageDirectory()), "CPURoofline/")) except: return None summaries = get_test_summaries() generate_roofline_plot(summaries) generate_bandwidth_plot(get_best_summary(summaries)) print("Plotting complete") @method(jvoid, []) def processGPURoofline(self): try: os.chdir( os.path.join(str(Environment.getExternalStorageDirectory()), "GPURoofline/")) except: return None summaries = get_test_summaries() generate_roofline_plot(summaries) generate_bandwidth_plot(get_best_summary(summaries)) print("Plotting complete")
class Modifiers(static_proxy()): @method(jboolean, [], modifiers="public synchronized") def synced(self): return Thread.holdsLock(self) @method(jboolean, [], modifiers="public") def unsynced(self): return Thread.holdsLock(self)
class MainActivityEx(static_proxy(AppCompatActivity)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main) @method(jint, [jint]) def func(self, num): return num + 1
class BasicAdder(static_proxy()): @constructor([jint]) def __init__(self, n): super(BasicAdder, self).__init__() self.n = n @method(jint, [jint]) def add(self, x): return self.n + x
class SplashActivity(static_proxy(AppCompatActivity)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) intent = Intent(self, scar.MainActivity.getClass()) self.startActivity(intent) self.finish()
class MainActivity(static_proxy(AppCompatActivity, View.OnClickListener)): @Override(jvoid, [Bundle]) def onCreate(self, state): self.getSystemService(Context.NOTIFICATION_SERVICE).cancelAll() AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main) ab = np.zeros((3, 4)) self.findViewById(R.id.btnCal).setOnClickListener(self) self.working = False self.findViewById(R.id.txtRes).setText("Res2" + str(ab)) @Override(jvoid, [View]) def onClick(self, v): if self.working: Toast.makeText(self, 'Plz Wait...', Toast.LENGTH_SHORT).show() return None self.working = True Toast.makeText(self, 'FftCal will save outputs in your storage', Toast.LENGTH_LONG).show() mdir = os.environ['EXTERNAL_STORAGE'] + '/FftCal/' if not os.path.exists(mdir): os.makedirs(mdir) t = np.arange(0.0, 105.0001, 0.01) Fs = 1 / (t[1] - t[0]) if os.path.isfile(mdir + 'input.csv'): S = np.loadtxt(mdir + 'input.csv') else: S = 10 * cos(2 * pi * 25 * t + pi / 6) + 5 * cos( 2 * pi * 50 * t + pi / 8 ) + 2 * cos(2 * pi * 75 * t + pi / 3) + 1 * cos( 2 * pi * 8 * t + pi / 7) + 1.5 * cos(2 * pi * 35 * t + pi / 5) print('S.shape', S.shape) [freq, xdft, psdx, phase, complexx] = Mide_FFT_PSD(S, Fs) np.savetxt(mdir + 'freq_xdft_psdx_phase.csv', np.column_stack((freq, xdft, psdx, phase)), delimiter=',') np.savetxt(mdir + 'input_fft.csv', np.column_stack((S, complexx)), delimiter=',') self.working = False
class OverloadedCtor(static_proxy()): @constructor([]) @constructor([jint]) @constructor([String]) def __init__(self, value=None): super().__init__() self.value = str(value) @method(String, []) def get(self): return self.value
class MainActivity (static_proxy(AppCompatActivity)): # Any methods that will be accessed from Java must implement # @Override(java_return_type, [method_args]) @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main) label = self.findViewById(R.id.label) label.setText("Prakruti is mast")
class ProtectedChild(static_proxy(SPT.ProtectedParent)): @method(jvoid, [String]) def setViaChildMethod(self, s): self.setViaParent(s) @method(jvoid, [String]) def setViaChildField(self, s): self.s = s @method(String, []) def getViaChildField(self): return self.s
class DataViewActivity(static_proxy(AppCompatActivity)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_data_view) intent = self.getIntent() id = intent.getIntExtra("id", 1) main_data = get_main_data(id) data_text_view = self.findViewById(R.id.data_text) data_text_view.setText(main_data[0]['text']) data_text_view.setMovementMethod(ScrollingMovementMethod())
class Return(static_proxy()): @method(jvoid, []) def void_good(self): pass @method(jvoid, []) def void_bad(self): return "hello" @method(jint, []) def primitive_good(self): return 42 @method(jint, []) def primitive_bad_value(self): return "hello" @method(jint, []) def primitive_bad_null(self): pass @method(String, []) def object_good_value(self): return "hello" @method(String, []) def object_good_null(self): return None @method(String, []) def object_bad(self): return 42 @method(jarray(String), []) def array_good_value(self): return ["hello", "world"] @method(jarray(String), []) def array_good_null(self): return None @method(jarray(String), []) def array_bad_content(self): return [1, 2] @method(jarray(String), []) def array_bad_value(self): return "hello"
class MenuFragment(static_proxy(PreferenceFragmentCompat)): @Override(jvoid, [Bundle, String]) def onCreatePreferences(self, state, rootKey): self.addPreferencesFromResource(R.xml.activity_ui_demo) from android.media import AudioManager, SoundPool self.sound_pool = SoundPool(1, AudioManager.STREAM_MUSIC, 0) self.sound_id = self.sound_pool.load(self.getActivity(), R.raw.sound, 1) @Override(jboolean, [Preference]) def onPreferenceTreeClick(self, pref): method = getattr(self, pref.getKey()) if method: method(self.getActivity()) return True else: return False def demo_dialog(self, activity): ColorDialog().show(self.getFragmentManager(), "color") def demo_notify(self, activity): from androidx.core.app import NotificationCompat builder = NotificationCompat.Builder(activity, App.DEFAULT_CHANNEL) builder.setSmallIcon(R.drawable.ic_launcher) builder.setContentTitle( activity.getString(R.string.demo_notify_title)) builder.setContentText( activity.getString(R.string.demo_notify_text)) activity.getSystemService(Context.NOTIFICATION_SERVICE)\ .notify(0, builder.getNotification()) def demo_toast(self, activity): from android.widget import Toast Toast.makeText(activity, R.string.demo_toast_text, Toast.LENGTH_SHORT).show() def demo_sound(self, activity): self.sound_pool.play(self.sound_id, 1, 1, 0, 0, 1) def demo_vibrate(self, activity): activity.getSystemService(Context.VIBRATOR_SERVICE)\ .vibrate(200)
class UIDemoActivity(static_proxy(AppCompatActivity)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) if state is None: state = Bundle() self.setContentView(R.layout.activity_menu) self.findViewById(R.id.tvCaption).setText(R.string.demo_caption) self.title_drawable = ColorDrawable() self.getSupportActionBar().setBackgroundDrawable(self.title_drawable) self.title_drawable.setColor( state.getInt("title_color", self.getResources().getColor(R.color.blue))) self.wvSource = self.findViewById(R.id.wvSource) view_source(self, self.wvSource, "ui_demo.py") self.wvSource.setVisibility( state.getInt("source_visibility", View.GONE)) self.getSupportFragmentManager().beginTransaction()\ .replace(R.id.flMenu, MenuFragment()).commit() @Override(jvoid, [Bundle]) def onSaveInstanceState(self, state): state.putInt("source_visibility", self.wvSource.getVisibility()) state.putInt("title_color", self.title_drawable.getColor()) @Override(jboolean, [Menu]) def onCreateOptionsMenu(self, menu): self.getMenuInflater().inflate(R.menu.view_source, menu) return True @Override(jboolean, [MenuItem]) def onOptionsItemSelected(self, item): id = item.getItemId() if id == R.id.menu_source: vis = self.wvSource.getVisibility() new_vis = View.VISIBLE if (vis == View.GONE) else View.GONE self.wvSource.setVisibility(new_vis) return True else: return False
class MainActivity(static_proxy(AppCompatActivity, View.OnClickListener)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main) self.topics = get_main_topics() main_topics_scroll = self.findViewById(R.id.main_topics_scroll) # class MainTopicButtonListener(dynamic_proxy(View.OnClickListener)): # def __init__(self, main_activity, **kwargs): # super(MainTopicButtonListener, self).__init__() # self.main_activity = main_activity # # @Override(jvoid, [View]) # def onClick(self, v): # #print(v.getId()) # intent = Intent(self.main_activity, data_view.DataViewActivity.getClass()) # intent.putExtra("id", v.getId()) # Activity.startActivity(intent) for topic in self.topics: topic_button = Button(self) topic_button.setText(topic['text']) topic_button.setId(topic['id']) params = GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1.0), GridLayout.spec(GridLayout.UNDEFINED, 1.0)) params.width = 0 topic_button.setLayoutParams(params) topic_button.setOnClickListener(self) main_topics_scroll.addView(topic_button) @Override(jvoid, [View]) def onClick(self, v): #print(v.getId()) intent = Intent(self, data_view.DataViewActivity.getClass()) intent.putExtra("id", jint(v.getId())) #intent.putExtra("topics", jarray((id, topic) for id, topic in self.topics.items())) self.startActivity(intent)
class ColorDialog(static_proxy(DialogFragment)): @Override(AlertDialog, [Bundle]) def onCreateDialog(self, state): activity = self.getActivity() builder = AlertDialog.Builder(activity) builder.setTitle(R.string.demo_dialog_title) builder.setMessage(R.string.demo_dialog_text) class Listener(dynamic_proxy(DialogInterface.OnClickListener)): def __init__(self, color_res): super(Listener, self).__init__() self.color = activity.getResources().getColor(color_res) def onClick(self, dialog, which): activity.title_drawable.setColor(self.color) builder.setNegativeButton(R.string.red, Listener(R.color.red)) builder.setNeutralButton(R.string.green, Listener(R.color.green)) builder.setPositiveButton(R.string.blue, Listener(R.color.blue)) return builder.create()
class Exceptions(static_proxy()): @method(jvoid, []) def undeclared_0(self): raise FileNotFoundException("fnf") @method(jvoid, [], throws=[EOFException]) def undeclared_1(self): raise FileNotFoundException("fnf") @method(jvoid, [], throws=[EOFException]) def declared(self): raise EOFException("eof") @method(jvoid, []) def python(self): raise TypeError("te") @method(jvoid, []) def error(self): raise Error("e") @method(jvoid, []) def runtime_exception(self): raise RuntimeException("re")
class MainActivity(static_proxy(AppCompatActivity)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main)
class GC(static_proxy()): @constructor([]) def __init__(self): super(GC, self).__init__() self.dt = DelTrigger()
class OtherPackage(static_proxy(package="other.pkg")): @method(String, []) def hello(self): return "world"
class CaIaIb(static_proxy(SPT.ClassA, SPT.IntA, SPT.IntB)): pass
class CaIa(static_proxy(SPT.ClassA, SPT.IntA)): pass
class IaIb(static_proxy(None, SPT.IntA, SPT.IntB)): pass
class Ia(static_proxy(None, SPT.IntA)): pass
class Ca(static_proxy(SPT.ClassA)): pass
class BasicTransformer(static_proxy()): @constructor([jint]) def __init__(self, w, h): super(BasicTransformer, self).__init__() self.width = w print("Python: w:" + str(w) + ', h:' + str(h)) self.height = h @method(jboolean, [jboolean]) def set_debug_init(self, debug_init): self.debug_init = debug_init return True @method(jarray(jint), [jarray(jbyte)]) def show_img(self, image_data): t = bytes(image_data) if (self.debug_init): print("Python: bytes ready") y = np.frombuffer(t, dtype=np.uint8) if (self.debug_init): print("Python: y:" + str(-y[:100])) y = np.array(list(map(lambda x: x[:3], (y).reshape((-1, 4))))) if (self.debug_init): print("Python: y:" + str(y[:3])) y = y.reshape((self.height, self.width, 3)) + 128 return array('l', list(y.reshape((-1)))) @method(jarray(jint), [jarray(jbyte)]) def get_corners(self, image_data): if (self.debug_init): print("Python: Start!") if (self.debug_init): print("Python: Start!") t = bytes(image_data) if (self.debug_init): print("Python: bytes ready") y = np.frombuffer(t, dtype=np.uint8) if (self.debug_init): print("Python: y:" + str(-y[:100])) y = y.reshape((self.height, self.width, 4)) y = y[:, :, :3] if (self.debug_init): print("Python: y:" + str(y[:3])) if (self.debug_init): print("Python: y.shape:" + str(y.shape)) print("Python: " + str(y.min()) + '< > ' + str(y.max())) print("Python: " + str(type(y))) print("Python: " + str(y.shape)) mas = am.get_corners(y) if (self.debug_init): print("Python: mas:" + str(mas)) print("Python: " + str(len(y))) if len(mas) == 0: mas = [[0]] else: if (self.debug_init): print("Python: mas: " + str(mas)) mas_ = [] for i in mas: mas_ += [i[1], i[0]] if (self.debug_init): print("Python: mas_: " + str(mas_)) return array('l', mas_)
class WrongExtends( static_proxy(package="com.chaquo.python.static_proxy")): pass
class MainActivity(static_proxy(AppCompatActivity)): @Override(jvoid, [Bundle]) def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main) self.findViewById(R.id.text_box_1).setText("Hello Python")
class WrongImplements( static_proxy(None, Runnable, package="com.chaquo.python.static_proxy")): pass
class OverloadedMethod(static_proxy()): @method(jint, [jint, jint]) @method(jdouble, [jdouble, jdouble]) @method(String, [String, String]) def add(self, a, b): return a + b
class OverrideChild(static_proxy(SPT.OverrideParent)): @Override(String, []) def get(self): return SPT.OverrideParent.get(self) + " child"