I bought myself a android car unit and came at the conclusion that the minimum brightness is still to bright when driving in the night. There is a clever app called screen filter that (i think it works like this) uses a overlay over the screen to fake a screen dimming. I wanted to make this by myself because i want to enable it when it's sunset and disable it after sunrise.
After some searching in the web i found a usable code. Create a Main activity with a service called Overlayservice. The service runs when i start the app and the overlay also works fine.
The next thing i want to make is a code so i can change the overlay transparency. I want that the program starts fading when sunset starts and for example after an half hour the transparency is 50%. The problem i am facing at is i am not able to set a new colour and transparency while the overlay is active in a service.
What i tired was creating a timer in the service that sets the colour after 5 seconds but i got a crash with the error:
Only the original thread that created a view hierarchy can touch its views.
The code i now have is as following:
package erik.autostart;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.LinearLayout;
import java.util.Timer;
import java.util.TimerTask;
public class OverlayService extends Service {
String color;
LinearLayout oView;
WindowManager.LayoutParams localLayoutParams;
private static Timer timer = new Timer();
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.color = intent.getExtras().getString("color");
Log.v("Message","Intent: " + Color.parseColor(this.color));
oView = new LinearLayout(getBaseContext());
localLayoutParams = new WindowManager.LayoutParams();
oView.setBackgroundColor(Color.parseColor(this.color)); //De eerste 2 waardes zijn de density ff is volledig en 00 is weg de laatste 6 waardes zijn hex color waardes, kan dus zwart zijn.
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.format = PixelFormat.TRANSLUCENT;
manager.addView(oView, localLayoutParams);
timer.scheduleAtFixedRate(new Task(), 0, 500);
return START_STICKY;
}
private class Task extends TimerTask
{
public void run()
{
Log.v("Message", "Tasker");
runtask();
}
}
void runtask()
{
if(oView!=null){
oView.setBackgroundColor(Color.parseColor("#ff000000"));
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.updateViewLayout(oView, oView.getLayoutParams());
}
}
@Override
public void onCreate() {
super.onCreate();
Log.v("Message", "OverlayService started");
}
@Override
public void onDestroy() {
if(oView!=null){
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(oView);
}
Log.v("Message", "Destroying OverlayService");
super.onDestroy();
}
}
I want to run a tasker somewhere so i can set the new colour and update the overlay. To fix this i thought perhaps stopping the service and starting it again with a new colour would help so i made a taker in the Mainactivity and stopped the service and start a new one with a different colour. The problem there is that the screen flickers.
I was hoping someone can help me with this problem. Is it even possible to do what i want?