Update theme submodule
[platform/upstream/gstreamer.git] / tutorials / android-tutorial-2 / src / org / freedesktop / gstreamer / tutorials / tutorial_2 / Tutorial2.java
1 package org.freedesktop.gstreamer.tutorials.tutorial_2;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.ImageButton;
9 import android.widget.TextView;
10 import android.widget.Toast;
11
12 import org.freedesktop.gstreamer.GStreamer;
13
14 public class Tutorial2 extends Activity {
15     private native void nativeInit();     // Initialize native code, build pipeline, etc
16     private native void nativeFinalize(); // Destroy pipeline and shutdown native code
17     private native void nativePlay();     // Set pipeline to PLAYING
18     private native void nativePause();    // Set pipeline to PAUSED
19     private static native boolean nativeClassInit(); // Initialize native class: cache Method IDs for callbacks
20     private long native_custom_data;      // Native code will use this to keep private data
21
22     private boolean is_playing_desired;   // Whether the user asked to go to PLAYING
23
24     // Called when the activity is first created.
25     @Override
26     public void onCreate(Bundle savedInstanceState)
27     {
28         super.onCreate(savedInstanceState);
29
30         // Initialize GStreamer and warn if it fails
31         try {
32             GStreamer.init(this);
33         } catch (Exception e) {
34             Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
35             finish(); 
36             return;
37         }
38
39         setContentView(R.layout.main);
40
41         ImageButton play = (ImageButton) this.findViewById(R.id.button_play);
42         play.setOnClickListener(new OnClickListener() {
43             public void onClick(View v) {
44                 is_playing_desired = true;
45                 nativePlay();
46             }
47         });
48
49         ImageButton pause = (ImageButton) this.findViewById(R.id.button_stop);
50         pause.setOnClickListener(new OnClickListener() {
51             public void onClick(View v) {
52                 is_playing_desired = false;
53                 nativePause();
54             }
55         });
56
57         if (savedInstanceState != null) {
58             is_playing_desired = savedInstanceState.getBoolean("playing");
59             Log.i ("GStreamer", "Activity created. Saved state is playing:" + is_playing_desired);
60         } else {
61             is_playing_desired = false;
62             Log.i ("GStreamer", "Activity created. There is no saved state, playing: false");
63         }
64
65         // Start with disabled buttons, until native code is initialized
66         this.findViewById(R.id.button_play).setEnabled(false);
67         this.findViewById(R.id.button_stop).setEnabled(false);
68
69         nativeInit();
70     }
71
72     protected void onSaveInstanceState (Bundle outState) {
73         Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired);
74         outState.putBoolean("playing", is_playing_desired);
75     }
76
77     protected void onDestroy() {
78         nativeFinalize();
79         super.onDestroy();
80     }
81
82     // Called from native code. This sets the content of the TextView from the UI thread.
83     private void setMessage(final String message) {
84         final TextView tv = (TextView) this.findViewById(R.id.textview_message);
85         runOnUiThread (new Runnable() {
86           public void run() {
87             tv.setText(message);
88           }
89         });
90     }
91
92     // Called from native code. Native code calls this once it has created its pipeline and
93     // the main loop is running, so it is ready to accept commands.
94     private void onGStreamerInitialized () {
95         Log.i ("GStreamer", "Gst initialized. Restoring state, playing:" + is_playing_desired);
96         // Restore previous playing state
97         if (is_playing_desired) {
98             nativePlay();
99         } else {
100             nativePause();
101         }
102
103         // Re-enable buttons, now that GStreamer is initialized
104         final Activity activity = this;
105         runOnUiThread(new Runnable() {
106             public void run() {
107                 activity.findViewById(R.id.button_play).setEnabled(true);
108                 activity.findViewById(R.id.button_stop).setEnabled(true);
109             }
110         });
111     }
112
113     static {
114         System.loadLibrary("gstreamer_android");
115         System.loadLibrary("tutorial-2");
116         nativeClassInit();
117     }
118
119 }