2

I'm trying to embed a widget into LockScreen. This widget has been implemented already and has put in "packages/apps". In LockScreen constructor ("framework/base/policy/src/com/android/internal/policy/impl/LockScreen.java), I added some code lines:

    if (mCreationOrientation != Configuration.ORIENTATION_LANDSCAPE) {
       inflater.inflate(R.layout.keyguard_screen_tab_unlock_advanced, this, true);
       showWeatherClockWidget();
    } else {
       inflater.inflate(R.layout.keyguard_screen_tab_unlock_land, this, true);
    }

I implemented showWeatherClockWidget() function as follows:

    private void showWeatherClockWidget() {
            mHost = new AppWidgetHost(mContext, HOST_ID);
            mAppWidgetManager = AppWidgetManager.getInstance(mContext);

            List<AppWidgetProviderInfo> providers = mAppWidgetManager.getInstalledProviders();
            for (AppWidgetProviderInfo pi: providers) {
                if (WEATHER_CLOCK_WIDGET.equals(pi.provider.getClassName())) {
                    int id = mHost.allocateAppWidgetId();
                    mPi = pi;
                    Log.v(TAG, "pi: " + pi.provider.getClassName());
                    Log.v(TAG, "id_length: " + mAppWidgetManager.getAppWidgetIds(pi.provider).length);
                    mAppWidgetManager.bindAppWidgetId(id, pi.provider);
                    addAppWidgetView(id, pi);
                }
            }
        }

   private void addAppWidgetView(int appWidgetId, AppWidgetProviderInfo appWidget) {
        // Inflate the AppWidget's RemoteViews
        mAppWidgetContainer = (LinearLayout) findViewById(R.id.weatherclock_widget_container);
        AppWidgetHostView view = mHost.createView(mContext, appWidgetId, appWidget);
        view.setAppWidget(appWidgetId, appWidget);

        // Add it to the list
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mAppWidgetContainer.addView(view, layoutParams);
    }

The LinearLayout containing widget was defined in my own xml layout file placed in "framework/base/core/res/res/layout"

    <LinearLayout
        android:id="@+id/weatherclock_widget_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
     />

After that, I faced with a list of problems: Firstly, I met an error announcing me that:

E/AndroidRuntime( 2614): java.lang.SecurityException: bindAppWidgetId appWidgetId=25 provider=ComponentInfo{com.sample.weatherclock/com.sample.weatherclock.ClockWidgetDigital}: User 1000 does not have android.permission.BIND_APPWIDGET.

I believe that BIND_APPWIDGET has protectionLevel = "signature|system". I wonder why LockScreen does not have this permission because it runs with system priority. I tried to add this permission into AndroidManifest.xml ("frameworks/base/core/res") but it didn't work. So, I did some tricks by disabling some code lines used for checking permission in "/base/services/java/com/android/server/AppWidgetServiceImpl.java"

public void bindAppWidgetId(int appWidgetId, ComponentName provider) {
//    mContext.enforceCallingPermission(android.Manifest.permission.BIND_APPWIDGET,
//        "bindAppWidgetId appWidgetId=" + appWidgetId + " provider=" + provider);
    bindAppWidgetIdImpl(appWidgetId, provider);
 }

After that, the widget could be binded but I was in face of another problem: my widget could not update. It updates only on the first time the lockscreen turns on. I put a log in onUpdate() and i founded that it was called whenever the time changes.

I/WeatherClock(  326): updateTime: 9:2
I/WeatherClock(  326): updateDate: 10/8 |4
I/WeatherClock(  326): ClockWidgetDigital onUpdate()

Besides, I even can not interact with it. When i touched it, I received the log:

I/ActivityManager(  147): START {act=android.intent.action.MAIN flg=0x10000000 cmp=com.sample.weatherclock/.WeatherSettingsActivity bnds=[10,50][471,317] u=0} from pid -1

but nothing happen, this activity can not start.

Can anybody help me? I was stuck by this for days :( Thanks in advance.

UPDATE It is not really that the widget can not start activity when I touch it. Actually, after I unlock screen, it has run already. It just can not unlock itself. Any idea for solving it?

4

0 回答 0