Skip to content

Commit 266a63f

Browse files
author
Michael Ritchie
committed
* Better support for multiple screens sizes
* Allow flip rotation for landscape * Fix for latitude and longitude settings switched * Made clock backwards compatible
1 parent ee2e4bf commit 266a63f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1771
-3795
lines changed

app/src/main/AndroidManifest.xml

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
<activity
3737
android:name=".ui.activities.MainActivity"
38-
android:screenOrientation="landscape"
38+
android:screenOrientation="sensorLandscape"
3939
android:icon="@mipmap/ic_launcher">
4040

4141
<!-- Launch activity as default from Android Studio -->
@@ -47,7 +47,7 @@
4747
</activity>
4848

4949
<activity android:name=".ui.activities.SettingsActivity"
50-
android:screenOrientation="landscape"/>
50+
android:screenOrientation="sensorLandscape"/>
5151

5252
<!-- Database Provider -->
5353
<provider
@@ -66,7 +66,8 @@
6666
<!-- MQTT Services -->
6767
<service android:name="org.eclipse.paho.android.service.MqttService"/>
6868

69-
<activity android:name=".ui.activities.LogActivity"/>
69+
<activity android:name=".ui.activities.LogActivity"
70+
android:screenOrientation="sensorLandscape"/>
7071

7172
</application>
7273

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/BaseActivity.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ public void run() {
9393

9494
public void resetInactivityTimer() {
9595
closeScreenSaver();
96-
inactivityHandler.removeCallbacks(inactivityCallback);
97-
inactivityHandler.postDelayed(inactivityCallback, INACTIVITY_TIMEOUT);
96+
if(inactivityHandler != null) {
97+
inactivityHandler.removeCallbacks(inactivityCallback);
98+
inactivityHandler.postDelayed(inactivityCallback, INACTIVITY_TIMEOUT);
99+
}
98100
}
99101

100102
public void stopDisconnectTimer(){
101103
hideDialog();
102-
inactivityHandler.removeCallbacks(inactivityCallback);
104+
if(inactivityHandler != null) {
105+
inactivityHandler.removeCallbacks(inactivityCallback);
106+
}
103107
}
104108

105109
@Override
@@ -194,7 +198,7 @@ public void showArmOptionsDialog(ArmOptionsView.ViewListener armListener) {
194198
hideDialog();
195199
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
196200
View view = inflater.inflate(R.layout.dialog_alarm_options, null, false);
197-
final ArmOptionsView optionsView = (ArmOptionsView) view.findViewById(R.id.armOptionsView);
201+
final ArmOptionsView optionsView = view.findViewById(R.id.armOptionsView);
198202
optionsView.setListener(armListener);
199203
dialog = new AlertDialog.Builder(BaseActivity.this)
200204
.setCancelable(true)
@@ -221,14 +225,8 @@ public void showAlarmDisableDialog(AlarmDisableView.ViewListener alarmCodeListen
221225

222226
public void showExtendedForecastDialog(Daily daily) {
223227
hideDialog();
224-
Rect displayRectangle = new Rect();
225-
Window window = getWindow();
226-
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
227-
228228
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
229229
View view = inflater.inflate(R.layout.dialog_extended_forecast, null, false);
230-
view.setMinimumWidth((int)(displayRectangle.width() * 0.7f));
231-
//view.setMinimumHeight((int)(displayRectangle.height() * 0.7f));
232230
final ExtendedForecastView extendedForecastView = view.findViewById(R.id.extendedForecastView);
233231
extendedForecastView.setExtendedForecast(daily, getConfiguration().getWeatherUnits());
234232
dialog = new AlertDialog.Builder(BaseActivity.this)
@@ -247,13 +245,8 @@ public void closeScreenSaver() {
247245
public void showScreenSaver() {
248246
if(screenSaverDialog != null && screenSaverDialog.isShowing()) return;
249247
inactivityHandler.removeCallbacks(inactivityCallback);
250-
Rect displayRectangle = new Rect();
251-
Window window = getWindow();
252-
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
253248
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
254249
View view = inflater.inflate(R.layout.dialog_screen_saver, null, false);
255-
//view.setMinimumWidth((int)(displayRectangle.width() * 1f));
256-
//view.setMinimumHeight((int)(displayRectangle.height() * 1f));
257250
final ScreenSaverView screenSaverView = view.findViewById(R.id.screenSaverView);
258251
screenSaverView.setScreenSaver(BaseActivity.this, getConfiguration().showScreenSaverModule(),
259252
getConfiguration().getImageSource(), getConfiguration().getImageFitScreen(),
@@ -273,4 +266,11 @@ public void onClick(View view) {
273266
.setView(view)
274267
.show();
275268
}
269+
270+
private Rect getDisplayRectangle() {
271+
Rect displayRectangle = new Rect();
272+
Window window = getWindow();
273+
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
274+
return displayRectangle;
275+
}
276276
}

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/ui/activities/MainActivity.java

+38-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import android.content.DialogInterface;
2222
import android.content.Intent;
23+
import android.os.Build;
2324
import android.os.Bundle;
2425
import android.view.Menu;
2526
import android.view.MenuItem;
@@ -69,7 +70,7 @@ void buttonSettingsClicked() {
6970
Intent intent = SettingsActivity.createStartIntent(MainActivity.this);
7071
startActivity(intent);
7172
} else {
72-
showAlarmDisableDialog(true);
73+
showAlarmDisableDialog(false);
7374
}
7475
}
7576

@@ -86,20 +87,21 @@ void buttonSleep() {
8687

8788
private SubscriptionDataTask subscriptionDataTask;
8889
private MqttAndroidClient mqttAndroidClient;
89-
90+
private View decorView;
91+
9092
@Override
9193
protected void onCreate(Bundle savedInstanceState) {
9294

9395
super.onCreate(savedInstanceState);
9496

97+
decorView = getWindow().getDecorView();
98+
9599
requestWindowFeature(Window.FEATURE_NO_TITLE);
96-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
97-
WindowManager.LayoutParams.FLAG_FULLSCREEN);
98100

99101
setContentView(R.layout.activity_main);
100102

101103
ButterKnife.bind(this);
102-
104+
103105
if(getConfiguration().isFirstTime()) {
104106
showAlertDialog(getString(R.string.dialog_first_time), new DialogInterface.OnClickListener() {
105107
@Override
@@ -135,6 +137,34 @@ protected void onDestroy() {
135137
}
136138
}
137139

140+
@Override
141+
public void onWindowFocusChanged(boolean hasFocus) {
142+
super.onWindowFocusChanged(hasFocus);
143+
if (hasFocus) {
144+
int visibility;
145+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
146+
visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
147+
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
148+
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
149+
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
150+
| View.SYSTEM_UI_FLAG_FULLSCREEN
151+
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
152+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
153+
visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
154+
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
155+
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
156+
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
157+
| View.SYSTEM_UI_FLAG_FULLSCREEN;
158+
159+
} else {
160+
visibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
161+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
162+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
163+
}
164+
decorView.setSystemUiVisibility(visibility);
165+
}
166+
}
167+
138168
@Override
139169
public boolean onCreateOptionsMenu(Menu menu) {
140170
getMenuInflater().inflate(R.menu.global, menu);
@@ -320,7 +350,9 @@ public void onCancel() {
320350
});
321351
} else {
322352
resetInactivityTimer(); // restart screen saver
323-
triggeredView.setVisibility(View.GONE);
353+
if(triggeredView != null) {
354+
triggeredView.setVisibility(View.GONE);
355+
}
324356
}
325357
}
326358

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/ui/fragments/ControlsFragment.java

+1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ private void showAlarmDisableDialog(boolean beep) {
317317
public void onComplete(int pin) {
318318
mListener.publishDisarmed();
319319
setDisarmedView();
320+
hideAlarmPendingView();
320321
hideDialog();
321322
}
322323
@Override

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/ui/fragments/InformationFragment.java

+42-21
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import butterknife.Bind;
4343
import butterknife.ButterKnife;
4444
import butterknife.OnClick;
45-
import timber.log.Timber;
4645

4746
import static android.os.Looper.getMainLooper;
4847
import static java.lang.Math.round;
@@ -60,6 +59,9 @@ public class InformationFragment extends BaseFragment {
6059
@Bind(R.id.conditionImage)
6160
ImageView conditionImage;
6261

62+
@Bind(R.id.timeText)
63+
TextView timeText;
64+
6365
@Bind(R.id.dateText)
6466
TextView dateText;
6567

@@ -75,6 +77,8 @@ void weatherLayoutClicked() {
7577

7678
private WeatherModule weatherModule;
7779
private Daily extendedDaily;
80+
private Handler weatherHandler;
81+
private Handler timeHandler;
7882

7983
public InformationFragment() {
8084
// Required empty public constructor
@@ -102,25 +106,9 @@ public void onCreate(Bundle savedInstanceState) {
102106
public void onViewCreated(View view, Bundle savedInstanceState) {
103107

104108
super.onViewCreated(view, savedInstanceState);
105-
106-
// start the clock
107-
final Handler someHandler = new Handler(getMainLooper());
108-
someHandler.postDelayed(new Runnable() {
109-
@Override
110-
public void run() {
111-
String currentDateString = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(new Date());
112-
dateText.setText(currentDateString);
113-
someHandler.postDelayed(this, DATE_INTERVAL);
114-
}
115-
}, 10);
116-
117-
// start the weather module
118-
if(getConfiguration().showWeatherModule() && getConfiguration().getDarkSkyKey() != null
119-
&& getConfiguration().getLatitude() != null && getConfiguration().getLongitude() != null) {
120-
connectWeatherModule();
121-
} else {
122-
weatherLayout.setVisibility(View.GONE);
123-
}
109+
weatherHandler = new Handler(getMainLooper());
110+
timeHandler = new Handler(getMainLooper());
111+
timeHandler.postDelayed(timeRunnable, 1000);
124112
}
125113

126114
@Override
@@ -136,6 +124,9 @@ public void onResume() {
136124
// this picks up changes made in the settings and connects weather if needed
137125
if(getConfiguration().showWeatherModule() && getConfiguration().getDarkSkyKey() != null
138126
&& getConfiguration().getLatitude() != null && getConfiguration().getLongitude() != null) {
127+
if(weatherHandler != null) {
128+
weatherHandler.removeCallbacks(weatherRunnable);
129+
}
139130
connectWeatherModule();
140131
} else {
141132
disconnectWeatherModule();
@@ -147,9 +138,35 @@ && getConfiguration().getLatitude() != null && getConfiguration().getLongitude()
147138
public void onDetach() {
148139
super.onDetach();
149140
disconnectWeatherModule();
141+
if(timeHandler != null) {
142+
timeHandler.removeCallbacks(timeRunnable);
143+
}
150144
}
145+
146+
private Runnable timeRunnable = new Runnable() {
147+
@Override
148+
public void run() {
149+
String currentDateString = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(new Date());
150+
String currentTimeString = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault()).format(new Date());
151+
dateText.setText(currentDateString);
152+
timeText.setText(currentTimeString);
153+
if(timeHandler != null) {
154+
timeHandler.postDelayed(timeRunnable, 1000);
155+
}
156+
}
157+
};
158+
159+
private Runnable weatherRunnable = new Runnable() {
160+
@Override
161+
public void run() {
162+
weatherHandler.postDelayed(weatherRunnable, DATE_INTERVAL);
163+
}
164+
};
151165

152166
private void disconnectWeatherModule() {
167+
if(weatherHandler != null) {
168+
weatherHandler.removeCallbacks(weatherRunnable);
169+
}
153170
if(weatherModule != null) {
154171
weatherModule.cancelDarkSkyHourlyForecast();
155172
}
@@ -162,7 +179,6 @@ private void connectWeatherModule() {
162179

163180
final String apiKey = getConfiguration().getDarkSkyKey();
164181
final String units = getConfiguration().getWeatherUnits();
165-
Timber.d("units: " + units);
166182
final String lat = getConfiguration().getLatitude();
167183
final String lon = getConfiguration().getLongitude();
168184
weatherModule.getDarkSkyHourlyForecast(apiKey, units, lat, lon, new WeatherModule.ForecastListener() {
@@ -173,6 +189,11 @@ public void onWeatherToday(String icon, double temperature, String summary) {
173189
String displayUnits = (units.equals( DarkSkyRequest.UNITS_US)? getString(R.string.text_f): getString(R.string.text_c));
174190
temperatureText.setText(getString(R.string.text_temperature, String.valueOf(round(temperature)), displayUnits));
175191
conditionImage.setImageDrawable(ResourcesCompat.getDrawable(getResources(), WeatherUtils.getIconForWeatherCondition(icon), getActivity().getTheme()));
192+
193+
// start the clock
194+
if(weatherHandler != null) {
195+
weatherHandler.postDelayed(weatherRunnable, DATE_INTERVAL);
196+
}
176197
}
177198

178199
@Override

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/ui/views/ScreenSaverView.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import android.view.View;
2626
import android.widget.ImageView;
2727
import android.widget.RelativeLayout;
28-
import android.widget.TextClock;
28+
import android.widget.TextView;
2929

3030
import com.squareup.picasso.Picasso;
3131
import com.thanksmister.iot.mqtt.alarmpanel.R;
@@ -36,7 +36,10 @@
3636
import com.thanksmister.iot.mqtt.alarmpanel.tasks.DarkSkyTask;
3737
import com.thanksmister.iot.mqtt.alarmpanel.tasks.InstagramTask;
3838

39+
import java.text.DateFormat;
40+
import java.util.Date;
3941
import java.util.List;
42+
import java.util.Locale;
4043
import java.util.Random;
4144

4245
import butterknife.Bind;
@@ -50,12 +53,13 @@ public class ScreenSaverView extends RelativeLayout {
5053
ImageView screenSaverImage;
5154

5255
@Bind(R.id.screenSaverClock)
53-
TextClock screenSaverClock;
56+
TextView screenSaverClock;
5457

5558
private InstagramTask task;
5659
private String userName;
5760
private boolean fitToScreen;
5861
private Handler rotationHandler;
62+
private Handler timeHandler;
5963
private Picasso picasso;
6064
private List<InstagramItem> itemList;
6165
private Context context;
@@ -95,6 +99,10 @@ protected void onDetachedFromWindow() {
9599
if(rotationHandler != null) {
96100
rotationHandler.removeCallbacks(delayRotationRunnable);
97101
}
102+
103+
if(timeHandler != null) {
104+
timeHandler.removeCallbacks(timeRunnable);
105+
}
98106
}
99107

100108
public void setScreenSaver(Context context, boolean useImageScreenSaver, String userName, boolean fitToScreen,
@@ -107,10 +115,15 @@ public void setScreenSaver(Context context, boolean useImageScreenSaver, String
107115
if(useImageScreenSaver && !TextUtils.isEmpty(userName) ) {
108116
screenSaverImage.setVisibility(View.VISIBLE);
109117
screenSaverClock.setVisibility(View.GONE);
118+
if(timeHandler != null) {
119+
timeHandler.removeCallbacks(timeRunnable);
120+
}
110121
startScreenSavor();
111122
} else { // use clock
112123
screenSaverImage.setVisibility(View.GONE);
113124
screenSaverClock.setVisibility(View.VISIBLE);
125+
timeHandler = new Handler();
126+
timeHandler.postDelayed(timeRunnable, 10);
114127
}
115128
}
116129

@@ -130,6 +143,17 @@ public void run() {
130143
}
131144
};
132145

146+
private Runnable timeRunnable = new Runnable() {
147+
@Override
148+
public void run() {
149+
String currentTimeString = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault()).format(new Date());
150+
screenSaverClock.setText(currentTimeString);
151+
if(timeHandler != null) {
152+
timeHandler.postDelayed(timeRunnable, 1000);
153+
}
154+
}
155+
};
156+
133157
private void startImageRotation() {
134158
if(picasso == null) {
135159
picasso = Picasso.with(context);

0 commit comments

Comments
 (0)