1
+ package com .manuito .tornpda ;
2
+
3
+ import android .app .PendingIntent ;
4
+ import android .appwidget .AppWidgetManager ;
5
+ import android .content .Context ;
6
+ import android .content .Intent ;
7
+ import android .content .SharedPreferences ;
8
+ import android .graphics .Color ;
9
+ import android .net .Uri ;
10
+ import android .os .Build ;
11
+ import android .text .Html ;
12
+ import android .view .View ;
13
+ import android .widget .RemoteViews ;
14
+ import androidx .annotation .NonNull ;
15
+ import es .antonborri .home_widget .HomeWidgetBackgroundIntent ;
16
+ import es .antonborri .home_widget .HomeWidgetProvider ;
17
+
18
+ public class HomeWidgetRankedWar extends HomeWidgetProvider {
19
+
20
+ @ Override
21
+ public void onUpdate (@ NonNull Context context , @ NonNull AppWidgetManager appWidgetManager , int [] appWidgetIds , @ NonNull SharedPreferences widgetData ) {
22
+ for (int widgetId : appWidgetIds ) {
23
+ try {
24
+ boolean isDarkMode = widgetData .getBoolean ("darkMode" , false );
25
+ int layoutId = isDarkMode ? R .layout .ranked_war_layout_large_dark : R .layout .ranked_war_layout_large ;
26
+ RemoteViews view = new RemoteViews (context .getPackageName (), layoutId );
27
+ loadRankedWarData (view , context , widgetData , isDarkMode );
28
+ appWidgetManager .updateAppWidget (widgetId , view );
29
+ } catch (Exception e ) {
30
+ e .printStackTrace ();
31
+ }
32
+ }
33
+ }
34
+
35
+ private void loadRankedWarData (RemoteViews view , Context context , SharedPreferences prefs , boolean isDarkMode ) {
36
+ setupHeader (view , context , prefs , isDarkMode );
37
+ setupClickListeners (view , context );
38
+ boolean widgetVisible = prefs .getBoolean ("rw_widget_visibility" , false );
39
+ view .setViewVisibility (R .id .rw_upcoming_layout , View .GONE );
40
+ view .setViewVisibility (R .id .rw_active_layout , View .GONE );
41
+ view .setViewVisibility (R .id .rw_finished_layout , View .GONE );
42
+ view .setViewVisibility (R .id .rw_no_war_layout , View .GONE );
43
+ if (!widgetVisible ) {
44
+ setupNoWarLayout (view , "No ranked war data" );
45
+ return ;
46
+ }
47
+ String state = prefs .getString ("rw_state" , "none" );
48
+ switch (state ) {
49
+ case "upcoming" :
50
+ setupUpcomingWarLayout (view , prefs );
51
+ break ;
52
+ case "active" :
53
+ setupActiveWarLayout (view , prefs );
54
+ break ;
55
+ case "finished" :
56
+ setupFinishedWarLayout (view , prefs );
57
+ break ;
58
+ default :
59
+ setupNoWarLayout (view , "No ranked war data" );
60
+ break ;
61
+ }
62
+ }
63
+
64
+ // setupHeader y setupClickListeners sin cambios
65
+ private void setupHeader (RemoteViews view , Context context , SharedPreferences prefs , boolean isDarkMode ) {
66
+ String lastUpdated = prefs .getString ("last_updated" , "Updating..." );
67
+ view .setTextViewText (R .id .rw_last_updated , "Updating..." .equals (lastUpdated ) ? "" : lastUpdated );
68
+ boolean reloadingNow = prefs .getBoolean ("reloading" , false );
69
+ view .setViewVisibility (R .id .rw_icon_reload_active , reloadingNow ? View .VISIBLE : View .GONE );
70
+ view .setViewVisibility (R .id .rw_icon_reload , reloadingNow ? View .GONE : View .VISIBLE );
71
+ int reloadIconColor = isDarkMode ? Color .parseColor ("#9E9E9E" ) : Color .parseColor ("#888888" );
72
+ view .setInt (R .id .rw_icon_reload , "setColorFilter" , reloadIconColor );
73
+
74
+ int playerChain = prefs .getInt ("rw_player_chain" , 0 );
75
+ view .setTextViewText (R .id .rw_header_center_text , "CHAINING" );
76
+ view .setTextColor (R .id .rw_header_center_text , Color .parseColor ("#F44336" ));
77
+ view .setInt (R .id .rw_chaining_indicator , "setBackgroundResource" , R .drawable .chaining_indicator_background );
78
+
79
+ view .setViewVisibility (R .id .rw_chaining_indicator , playerChain >= 10 ? View .VISIBLE : View .GONE );
80
+ }
81
+
82
+ private void setupClickListeners (RemoteViews view , Context context ) {
83
+ PendingIntent reloadIntent = HomeWidgetBackgroundIntent .INSTANCE .getBroadcast (context , Uri .parse ("pdaWidget://reload_clicked" ), "Reloading..." );
84
+ view .setOnClickPendingIntent (R .id .rw_reload_box , reloadIntent );
85
+ Intent openAppIntent = new Intent (context , MainActivity .class );
86
+ openAppIntent .setData (Uri .parse ("pdaWidget://open:app" ));
87
+ PendingIntent pendingIntent = PendingIntent .getActivity (context , 0 , openAppIntent , PendingIntent .FLAG_UPDATE_CURRENT | getImmutableFlag ());
88
+ view .setOnClickPendingIntent (R .id .rw_widget_container , pendingIntent );
89
+ }
90
+
91
+ private void setupUpcomingWarLayout (RemoteViews view , SharedPreferences prefs ) {
92
+ view .setViewVisibility (R .id .rw_upcoming_layout , View .VISIBLE );
93
+ String countdown = prefs .getString ("rw_countdown_string" , "Loading..." );
94
+ String date = prefs .getString ("rw_date_string" , "" );
95
+ String playerTag = decodeHtml (prefs .getString ("rw_player_faction_tag" , "" ));
96
+ String enemyName = decodeHtml (prefs .getString ("rw_enemy_faction_name" , "" ));
97
+ boolean upcomingSoon = prefs .getBoolean ("rw_upcoming_soon" , false );
98
+
99
+ view .setInt (R .id .rw_upcoming_icon , "setColorFilter" , Color .parseColor ("#FFA500" ));
100
+ view .setTextViewText (R .id .rw_upcoming_countdown , countdown );
101
+ view .setTextViewText (R .id .rw_upcoming_date , date );
102
+ view .setTextViewText (R .id .rw_upcoming_player_tag , playerTag );
103
+ view .setTextViewText (R .id .rw_upcoming_enemy_name , enemyName );
104
+
105
+ if (upcomingSoon ) {
106
+ view .setTextColor (R .id .rw_upcoming_countdown , Color .parseColor ("#FFA500" ));
107
+ view .setInt (R .id .rw_upcoming_border_box , "setBackgroundColor" , Color .parseColor ("#FFA500" ));
108
+ } else {
109
+ view .setInt (R .id .rw_upcoming_border_box , "setBackgroundColor" , Color .TRANSPARENT );
110
+ view .setTextColor (R .id .rw_upcoming_countdown , Color .parseColor ("#000000" ));
111
+ }
112
+ }
113
+
114
+ private void setupActiveWarLayout (RemoteViews view , SharedPreferences prefs ) {
115
+ view .setViewVisibility (R .id .rw_active_layout , View .VISIBLE );
116
+ int playerScore = prefs .getInt ("rw_player_score" , 0 );
117
+ int enemyScore = prefs .getInt ("rw_enemy_score" , 0 );
118
+ int targetScore = prefs .getInt ("rw_target_score" , 1 );
119
+ String playerTag = decodeHtml (prefs .getString ("rw_player_faction_tag" , "" ));
120
+ String enemyName = decodeHtml (prefs .getString ("rw_enemy_faction_name" , "" ));
121
+ int progress = Math .abs (playerScore - enemyScore );
122
+ double percentageValue = (targetScore > 0 ) ? ((double ) progress * 100.0 ) / targetScore : 0.0 ;
123
+ String percentageText = String .format ("%.1f%%" , percentageValue );
124
+ view .setTextViewText (R .id .rw_active_player_tag , playerTag );
125
+ view .setTextViewText (R .id .rw_active_enemy_name , enemyName );
126
+ view .setTextViewText (R .id .rw_active_player_score , String .format ("%,d" , playerScore ));
127
+ view .setTextViewText (R .id .rw_active_enemy_score , String .format ("%,d" , enemyScore ));
128
+ view .setTextViewText (R .id .rw_active_progress_text , String .format ("%d / %d" , progress , targetScore ));
129
+ view .setTextViewText (R .id .rw_active_percentage , percentageText );
130
+ int greenColor = Color .parseColor ("#4CAF50" );
131
+ int redColor = Color .parseColor ("#F44336" );
132
+ view .setTextColor (R .id .rw_active_player_score , playerScore >= enemyScore ? greenColor : redColor );
133
+ view .setTextColor (R .id .rw_active_enemy_score , enemyScore > playerScore ? greenColor : redColor );
134
+ view .setProgressBar (R .id .rw_active_progress_bar , targetScore , progress , false );
135
+ }
136
+
137
+ private void setupFinishedWarLayout (RemoteViews view , SharedPreferences prefs ) {
138
+ view .setViewVisibility (R .id .rw_finished_layout , View .VISIBLE );
139
+
140
+ int playerScore = prefs .getInt ("rw_player_score" , 0 );
141
+ int enemyScore = prefs .getInt ("rw_enemy_score" , 0 );
142
+ String playerTag = decodeHtml (prefs .getString ("rw_player_faction_tag" , "" ));
143
+ String enemyName = decodeHtml (prefs .getString ("rw_enemy_faction_name" , "" ));
144
+ String endDate = prefs .getString ("rw_end_date_string" , "" );
145
+ boolean playerWon = playerScore >= enemyScore ;
146
+ int resultColor = playerWon ? Color .parseColor ("#4CAF50" ) : Color .parseColor ("#F44336" );
147
+
148
+ String resultText = playerWon ? "Won" : "Lost" ;
149
+ String finalDisplayText = resultText + " " + endDate ;
150
+
151
+ view .setInt (R .id .rw_finished_border_box , "setBackgroundColor" , resultColor );
152
+
153
+ view .setTextViewText (R .id .rw_finished_winner_name , finalDisplayText );
154
+ view .setTextColor (R .id .rw_finished_winner_name , resultColor );
155
+
156
+ view .setImageViewResource (R .id .rw_finished_icon , R .drawable .trophy );
157
+ view .setInt (R .id .rw_finished_icon , "setColorFilter" , resultColor );
158
+
159
+ view .setTextViewText (R .id .rw_finished_player_tag , playerTag );
160
+ view .setTextColor (R .id .rw_finished_player_tag , Color .parseColor ("#0D47A1" ));
161
+
162
+ view .setTextViewText (R .id .rw_finished_enemy_name , enemyName );
163
+ view .setTextColor (R .id .rw_finished_enemy_name , Color .parseColor ("#B71C1C" ));
164
+
165
+ view .setTextViewText (R .id .rw_finished_player_score , String .format ("%,d" , playerScore ));
166
+ view .setTextColor (R .id .rw_finished_player_score , playerWon ? resultColor : Color .parseColor ("#666666" ));
167
+
168
+ view .setTextViewText (R .id .rw_finished_enemy_score , String .format ("%,d" , enemyScore ));
169
+ view .setTextColor (R .id .rw_finished_enemy_score , !playerWon ? resultColor : Color .parseColor ("#666666" ));
170
+ }
171
+
172
+ private void setupNoWarLayout (RemoteViews view , String message ) {
173
+ view .setViewVisibility (R .id .rw_no_war_layout , View .VISIBLE );
174
+ view .setTextViewText (R .id .rw_no_war_text , message );
175
+ }
176
+
177
+ private int getImmutableFlag () {
178
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
179
+ return PendingIntent .FLAG_IMMUTABLE ;
180
+ } else {
181
+ return 0 ;
182
+ }
183
+ }
184
+
185
+ private String decodeHtml (String source ) {
186
+ if (source == null || source .isEmpty ()) return "" ;
187
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
188
+ return Html .fromHtml (source , Html .FROM_HTML_MODE_LEGACY ).toString ();
189
+ } else {
190
+ return Html .fromHtml (source ).toString ();
191
+ }
192
+ }
193
+ }
0 commit comments