termux_gui/
activity.rs

1//! Activity management
2
3use serde_json::{json, Value};
4use std::os::unix::net::UnixStream;
5
6use crate::connection::Connection;
7use crate::error::Result;
8use crate::components::*;
9
10/// Represents a GUI Activity (window)
11pub struct Activity {
12    conn: Connection,
13    aid: i64,
14}
15
16impl Activity {
17    /// Create a new Activity
18    /// 
19    /// # Arguments
20    /// * `dialog` - If true, creates a dialog-style window; if false, creates a full-screen activity
21    pub fn new(dialog: bool) -> Result<Self> {
22        eprintln!("[DEBUG] Activity::new() - creating connection...");
23        let mut conn = Connection::new()?;
24        
25        eprintln!("[DEBUG] Activity::new() - sending newActivity...");
26        let response = conn.send_read(&json!({
27            "method": "newActivity",
28            "params": {
29                "dialog": dialog,
30                "canceloutside": !dialog
31            }
32        }))?;
33        
34        eprintln!("[DEBUG] Activity::new() - got response: {:?}", response);
35        // Response is an array, aid is at index 0
36        let aid = response[0]
37            .as_i64()
38            .ok_or_else(|| crate::error::GuiError::InvalidResponse("Missing aid".to_string()))?;
39        
40        eprintln!("[DEBUG] Activity::new() - aid = {}", aid);
41        Ok(Activity { conn, aid })
42    }
43    
44    /// Get the Activity ID
45    pub fn id(&self) -> i64 {
46        self.aid
47    }
48    
49    /// Send a message and read response
50    pub fn send_read(&mut self, msg: &Value) -> Result<Value> {
51        self.conn.send_read(msg)
52    }
53    
54    /// Send a message without waiting for response
55    pub fn send(&mut self, msg: &Value) -> Result<()> {
56        self.conn.send(msg)
57    }
58    
59    /// Get mutable reference to event stream
60    pub fn event_stream(&mut self) -> &mut UnixStream {
61        self.conn.event_stream()
62    }
63    
64    /// Create a LinearLayout
65    pub fn create_linear_layout(&mut self, parent: Option<i64>) -> Result<LinearLayout> {
66        LinearLayout::new(self, parent)
67    }
68    
69    /// Create a LinearLayout with specified orientation
70    pub fn create_linear_layout_horizontal(&mut self, parent: Option<i64>) -> Result<LinearLayout> {
71        LinearLayout::new_with_orientation(self, parent, false)
72    }
73    
74    /// Create a NestedScrollView
75    pub fn create_nested_scroll_view(&mut self, parent: Option<i64>) -> Result<NestedScrollView> {
76        NestedScrollView::new(self, parent)
77    }
78    
79    /// Create a TextView
80    pub fn create_text_view(&mut self, text: &str, parent: Option<i64>) -> Result<TextView> {
81        TextView::new(self, text, parent)
82    }
83    
84    /// Create a Button
85    pub fn create_button(&mut self, text: &str, parent: Option<i64>) -> Result<Button> {
86        Button::new(self, text, parent)
87    }
88    
89    /// Create an EditText
90    pub fn create_edit_text(&mut self, text: &str, parent: Option<i64>) -> Result<EditText> {
91        EditText::new(self, text, parent)
92    }
93    
94    /// Create a multi-line EditText
95    pub fn create_edit_text_multiline(&mut self, text: &str, parent: Option<i64>) -> Result<EditText> {
96        EditText::new_multiline(self, text, parent)
97    }
98    
99    /// Create a Checkbox
100    pub fn create_checkbox(&mut self, text: &str, parent: Option<i64>) -> Result<Checkbox> {
101        Checkbox::new(self, text, parent)
102    }
103    
104    /// Create a Checkbox with initial checked state
105    pub fn create_checkbox_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<Checkbox> {
106        Checkbox::new_with_checked(self, text, parent, checked)
107    }
108    
109    /// Create a Switch
110    pub fn create_switch(&mut self, text: &str, parent: Option<i64>) -> Result<Switch> {
111        Switch::new(self, text, parent)
112    }
113    
114    /// Create a Switch with specified checked state
115    pub fn create_switch_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<Switch> {
116        Switch::new_with_checked(self, text, parent, checked)
117    }
118    
119    /// Create a RadioButton
120    pub fn create_radio_button(&mut self, text: &str, parent: Option<i64>) -> Result<RadioButton> {
121        RadioButton::new(self, text, parent)
122    }
123    
124    /// Create a RadioButton with specified checked state
125    pub fn create_radio_button_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<RadioButton> {
126        RadioButton::new_with_checked(self, text, parent, checked)
127    }
128    
129    /// Create a RadioGroup
130    pub fn create_radio_group(&mut self, parent: Option<i64>) -> Result<RadioGroup> {
131        RadioGroup::new(self, parent)
132    }
133    
134    /// Create a Spinner
135    pub fn create_spinner(&mut self, parent: Option<i64>) -> Result<Spinner> {
136        Spinner::new(self, parent)
137    }
138    
139    /// Create an ImageView
140    pub fn create_image_view(&mut self, parent: Option<i64>) -> Result<ImageView> {
141        ImageView::new(self, parent)
142    }
143    
144    /// Create a ProgressBar
145    pub fn create_progress_bar(&mut self, parent: Option<i64>) -> Result<ProgressBar> {
146        ProgressBar::new(self, parent)
147    }
148    
149    /// Create a ToggleButton
150    pub fn create_toggle_button(&mut self, text: &str, parent: Option<i64>) -> Result<ToggleButton> {
151        ToggleButton::new(self, text, parent)
152    }
153    
154    /// Create a ToggleButton with specified checked state
155    pub fn create_toggle_button_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<ToggleButton> {
156        ToggleButton::new_with_checked(self, text, parent, checked)
157    }
158    
159    /// Create a Space (empty space for layout)
160    pub fn create_space(&mut self, parent: Option<i64>) -> Result<Space> {
161        Space::new(self, parent)
162    }
163    
164    /// Create a FrameLayout
165    pub fn create_frame_layout(&mut self, parent: Option<i64>) -> Result<FrameLayout> {
166        FrameLayout::new(self, parent)
167    }
168    
169    /// Create a GridLayout with specified rows and columns
170    pub fn create_grid_layout(&mut self, rows: i32, cols: i32, parent: Option<i64>) -> Result<GridLayout> {
171        GridLayout::new(self, rows, cols, parent)
172    }
173    
174    /// Create a HorizontalScrollView
175    pub fn create_horizontal_scroll_view(&mut self, parent: Option<i64>) -> Result<HorizontalScrollView> {
176        HorizontalScrollView::new(self, parent)
177    }
178    
179    /// Create a HorizontalScrollView with custom parameters
180    pub fn create_horizontal_scroll_view_with_params(&mut self, parent: Option<i64>,
181                                                     fillviewport: bool, snapping: bool, nobar: bool) -> Result<HorizontalScrollView> {
182        HorizontalScrollView::new_with_params(self, parent, fillviewport, snapping, nobar)
183    }
184    
185    /// Create a SwipeRefreshLayout
186    pub fn create_swipe_refresh_layout(&mut self, parent: Option<i64>) -> Result<SwipeRefreshLayout> {
187        SwipeRefreshLayout::new(self, parent)
188    }
189    
190    /// Create a TabLayout
191    pub fn create_tab_layout(&mut self, parent: Option<i64>) -> Result<TabLayout> {
192        TabLayout::new(self, parent)
193    }
194    
195    /// Create a WebView
196    pub fn create_web_view(&mut self, parent: Option<i64>) -> Result<WebView> {
197        WebView::new(self, parent)
198    }
199    
200    /// Set the Activity title
201    pub fn set_title(&mut self, title: &str) -> Result<()> {
202        self.send(&json!({
203            "method": "setTheme",
204            "params": {
205                "aid": self.aid,
206                "statusBarColor": 0,
207                "colorPrimary": 0,
208                "windowBackground": 0,
209                "textColor": 0,
210                "colorAccent": 0
211            }
212        }))?;
213        
214        self.send_read(&json!({
215            "method": "setTaskDescription",
216            "params": {
217                "aid": self.aid,
218                "label": title
219            }
220        }))?;
221        
222        Ok(())
223    }
224    
225    /// Finish (close) the Activity
226    pub fn finish(&mut self) -> Result<()> {
227        self.send_read(&json!({
228            "method": "finishActivity",
229            "params": {
230                "aid": self.aid,
231                "finishing": true
232            }
233        }))?;
234        Ok(())
235    }
236}