3

I've been fiddling with the new DreamService API for the past few hours, and it's pretty sweet. I do have one question, though.

I'd like to lock the screen orientation to landscape when the DreamService is shown. The purpose of the DreamService is to show a number of widescreen photos, and I'd rather not have the black bars at the top and bottom of the screen.

I've tried a number of things, including setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) in Java code, and android:screenOrientation="landscape" in the manifest file, but none have worked.

This makes me wonder: Is it possible to lock the orientation of a DreamService?

Michell Bak
  • 13,182
  • 11
  • 64
  • 121

3 Answers3

2

The Android developer team confirmed that this currently isn't possible during a live developer Hangout some time ago.

I believe Google wants DreamServices to work in both screen orientations, so that's why it isn't possible.

Michell Bak
  • 13,182
  • 11
  • 64
  • 121
1

Workaround: use this lib https://github.com/rongi/rotate-layout

build.gradle

 dependencies {
        compile 'rongi.rotate-layout:rotate-layout:2.0.0'
    }

your_daydream_layout.xml

<com.github.rongi.rotate_layout.layout.RotateLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:angle="-90">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
        android:id="@+id/daydream_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2a2e31" />

    <TextView
        android:id="@+id/dream_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:text="..."
            android:textColor="#abc"
            android:textSize="20sp" />
    </RelativeLayout>
</com.github.rongi.rotate_layout.layout.RotateLayout>

And btw: Allow to auto rotate your daydream .. from https://code.google.com/p/android/issues/detail?id=40226

"It looks like a fix for this has been implemented (and nobody told us). Unfortunately, it doesn't work properly. On my Nexus 6p (6.0.1, Build #MHC19Q), if I swipe to the left-most screen (Google Now?), hit the hamburger menu at the upper left, select Settings, and go to the bottom, there's an "Allow Rotation" of the home screen option. With that enabled, the home screen will then be able to rotate to landscape orientation when needed. Daydream can then inherit that rotation and display in landscape mode, too." -- May 18, 2016

Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35
0

It's a shame that I didn't found any solution either, I had to use a nasty workaround which may or may not work for you, it is, setting the auto-rotation setting off, right before setting your layout:

In your DreamService class:

import android.provider.Settings;
<...>

    @Override
public void onAttachedToWindow() {

    Settings.System.putInt(getContentResolver(),
            android.provider.Settings.System.ACCELEROMETER_ROTATION, 0);

    setContentView(R.layout.dream_main);
<...>

In your manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
frapeti
  • 1,090
  • 13
  • 18