Android Question Sending Offset to Wallpaper (For Launcher)

treehousefrog

Member
Licensed User
Longtime User
Hey guys!

So I'm creating a homescreen launcher (or two actually...) and want to support multiple screens of course. The problem is, that livewallpapers and images don't pick up when the user changes the screen they're looking at (I'm using AHPager as the basis - it's very well suited to the task!) and instead just stay static.

I've looked around and there's a little discussion on the matter but no resolution. I think that my launcher app needs to somehow send an intent or something when the user switches page - like xOffSetStep + 1 (or something :p) and I was wondering if this could be done with reflection library??

Otherwise, any other suggestions?

Thanks a lot in advance, this will save my life :-D
 

treehousefrog

Member
Licensed User
Longtime User
Yes :) I'm hoping to send the information each time the user changes page in the AHPager so that the wallpaper knows to scroll. Thanks again!
 
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
Ah sorry :) No I've just done this in the manifest:

SetActivityAttribute(main, android:theme, @android:style/Theme.Wallpaper)

Thanks!

*I guess hosting was the wrong word sorry...
 
Last edited:
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
After just a few hours of browsing :p I think this will work:

B4X:
Sub Pager_PageChanged (Position As Int)
Dim r As Reflector
r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))
Dim x As Float
Dim y As Float
x = 1 / Position
y = 1.0
r.RunMethod4("setWallpaperOffsets", Array As Object(Null, x, y), _
      Array As String("android.os.IBinder", "java.lang.float", "java.lang.float"))

The problem is that 'requested window null does not exist'. The Android documentation says I need to use 'View.getWindowToken()' to get an 'ibinder' for a view... but I'm a bit lost on how to do that... I'm guessing that's more reflection library but I'm still finding my way around it. Could anyone possibly help out...?
 
Last edited:
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
Okay I'm nearly there now...

B4X:
Sub Pager_PageChanged (Position As Int)
    Log ("Page Changed to " & Position)
    CurrentPage = Position
    SetButtonText
   
    Dim r As Reflector
   r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))

 x = (1.00 / 5) * (Position)
 y = 1.0

   Dim o As Reflector
   o.Target = Activity
   r.RunMethod4("setWallpaperOffsets", Array As Object(o.RunMethod("getWindowToken"), x, y), _
Array As String("android.os.IBinder", "java.lang.float", "java.lang.float"))

End Sub

I managed to get the IBinder for my activity and now the live wallpaper or static wallpaper move when you swipe between pages (in activity create I set the number of offsets to 1 / 5 as I have five homescreens). The only problem with this is that it's a sudden jump instead of a smooth transition. If there was a way I could get the token of a panel rather than the activity then I think I could solve this... but replacing activity with 'panel1' doesn't seem to work... anyone got any suggestions?

Thanks again, sorry for posting so much on this :)
 
Upvote 0

treehousefrog

Member
Licensed User
Longtime User
That's what I ended up doing :) Thanks alot!

For anyone else who may need this in the future...

B4X:
Sub Activity_Create(FirstTime As Boolean)

Dim r As Reflector
   r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))
  
r.RunMethod4("suggestDesiredDimensions", Array As Object(Activity.width * 2, Activity.height), _
Array As String("java.lang.int", "java.lang.int"))

Dim pagesx As Float
Dim pagesy As Float
pagesx = 1.00 / 5
pagesy = 1.00
r.RunMethod4("setWallpaperOffsetSteps", Array As Object(pagesx, pagesy), _
Array As String("java.lang.float", "java.lang.float"))

End Sub

Sub Pager_PageChanged (Position As Int)
    Log ("Page Changed to " & Position)
    CurrentPage = Position
    SetButtonText

targetx = (1.00 / 5) * (Position)
times = 0
If targetx > x Then
increments = (targetx - x) / 10
up = 1
Else
increments = (x - targetx) / 10
up = 0
End If

End Sub

Sub timer1_Tick

If times = 10 Then
x = targetx

    Dim r As Reflector
   r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))


   Dim o As Reflector
   o.Target = pager
   r.RunMethod4("setWallpaperOffsets", Array As Object(o.RunMethod("getWindowToken"), x, y), _
Array As String("android.os.IBinder", "java.lang.float", "java.lang.float"))

times = 11
End If

If targetx <> x Then
times = times + 1
If targetx > x Then
x = x + increments
Else If targetx < x Then
x = x - increments
End If

    Dim r As Reflector
   r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
      Array As Object(r.GetContext), Array As String("android.content.Context"))


   Dim o As Reflector
   o.Target = pager
   r.RunMethod4("setWallpaperOffsets", Array As Object(o.RunMethod("getWindowToken"), x, y), _
Array As String("android.os.IBinder", "java.lang.float", "java.lang.float"))



End If
End Sub

I know my code is mega-ugly :p But you get the drift... :-D
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I've converted that into reusable code:

B4X:
'XpageSize: 1/(numberofpages-1), YpageSize=1 usually
Sub LWP_SetDesiredDimensions(VirtualWidth As Int, VirtualHeight As Int, XpageSize As Float, YpageSize As Float)
    Dim r As Reflector'Must add this permission to the manifest: AddPermission("android.permission.SET_WALLPAPER_HINTS") and possibly android.permission.SET_WALLPAPER
      r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", Array As Object(r.GetContext), Array As String("android.content.Context"))
    r.RunMethod4("suggestDesiredDimensions", Array As Object(VirtualWidth, VirtualHeight), Array As String("java.lang.int", "java.lang.int"))
    r.RunMethod4("setWallpaperOffsetSteps", Array As Object(XpageSize, YpageSize), Array As String("java.lang.float", "java.lang.float"))
End Sub
'Dest: The object that will be hosting the wallpaper (ie: the activity)
Sub LWP_SetLocation(Dest As Object, X As Float, Y As Float)
    Dim r As Reflector, o As Reflector
      r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", Array As Object(r.GetContext), Array As String("android.content.Context"))
      o.Target = Dest
      r.RunMethod4("setWallpaperOffsets", Array As Object(o.RunMethod("getWindowToken"), X, Y), Array As String("android.os.IBinder", "java.lang.float", "java.lang.float"))
End Sub
 
Upvote 0
Top