6

I am trying to create a texture view in Service, and I dont seem to get "onSurfaceTextureAvailable" call back. "A TextureView or a subclass can only be used with hardware acceleration enabled." comes up in the log when I have provided android:hardwareAccelerated="true" for the application, is there any permission to be explicity provided to the service

package com.example.textures;



import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FlyMaadi extends Service implements SurfaceTextureListener {


    private final String TAG = "FlyMaadi";
    private TextureView mTextureView;
    private WindowManager mWindowManager;
    private LayoutInflater minflater;
    private ViewGroup mrel_layout;
    private TextureView mTextureView1;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    public void createSurfaceTexture()
    {
        Log.d(TAG,"Create Surface Texture\n");
        mTextureView = new TextureView(this);

        Log.d(TAG,"Is hardware accelerated:: "+ mTextureView.isHardwareAccelerated());


        mTextureView.setSurfaceTextureListener(this);

        //mTextureView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

        minflater = (LayoutInflater)getSystemService
                (LAYOUT_INFLATER_SERVICE);



        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        FrameLayout mParentView = new FrameLayout(getApplicationContext());


        final WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params1.width = 352;
        params1.height = 288;

        //mWindowManager.addView(mrel_layout, params1);

        mWindowManager.addView(mParentView, params1);


        if(mTextureView.isAvailable())
        {
            Log.d(TAG,"Texture view is already available");
        }
        else
        {
            Log.d(TAG,"Texture view is not available");
        }


        Log.d(TAG,"Adding surface texture:: ");


        mParentView.addView(mTextureView);






    }


    @Override 
    public void onCreate() {
        super.onCreate();

        Log.d(TAG,"onCreate");
        createSurfaceTexture();

    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureAvailable");
    }


    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureDestroyed");
        return false;
    }


    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureSizeChanged");

    }


    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // TODO Auto-generated method stub
        Log.d(TAG,"onSurfaceTextureUpdated");

    }

}

My Androidmanifest xml file is as below

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textures"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"        
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true" 
        >
        <activity
            android:name="com.example.textures.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service            
            android:name="com.example.textures.FlyMaadi"
            />


    </application>

</manifest>
user2315052
  • 61
  • 1
  • 4
  • did you try extending IntentService instead of the regular Service? It "may" be that since the Service is running on the main thread, you are having this issue. Other than that, check the WindowManager instance's properties. – VJ Vélan Solutions Feb 06 '14 at 04:59

3 Answers3

6

For "A TextureView or a subclass can only be used with hardware acceleration enabled.", you might want to do replace this line:

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

with

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED

HTH.

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
1

onSurfaceTextureAvailable will be called once the view is visible on the screen. Till then it will not be called. I see that you are not adding the textureview (and its parent) to the activity window or any of its views. Try passing an active view id or a view to the service and add the textureview into that. Once the view is displayed the called should be called . (BTW handling a view from a service is not recommended)

Shrish
  • 739
  • 5
  • 15
0

My problem was that I am using AndroidAnnotations and I had to handle everything without androidannotations in this class.

Marius Hilarious
  • 855
  • 9
  • 18