Message Boards

How to get android screenlet offline mode working for app?

Amit Sinha, modified 7 Years ago.

How to get android screenlet offline mode working for app?

Junior Member Posts: 43 Join Date: 12/31/14 Recent Posts
Hi everyone,
My application based on android screenlet is working fine.
now i want to run it in offline mode.

Followed below url as directed
https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/using-offline-mode-in-android

in android manifest xml file, added below codes
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <receiver android:name="com.liferay.mobile.screens.cache.CacheReceiver">..</receiver>
- <service android:name="com.liferay.mobile.screens.cache.CacheSyncService" android:exported="false"/>

and changed 'REMOTE_ONLY' into 'REMOTE_FIRST' in ListScreenlet java file
Integer offlinePolicy = typedArray.getInteger( R.styleable.WebContentListScreenlet_offlinePolicy, OfflinePolicy.REMOTE_FIRST.ordinal());

but i do not know
- from where to get SyncManager class,
- where to use "SessionContext.currentCacheManager" and context.sendBroadcast(new Intent("com.liferay.mobile.screens.cache.resync"));

Is there any need of 'Liferay CE Sync Connector' for offline mode. i have deployed it in server.

If i run the application, log in it and surf with various activities which retrieves data from database.
now when i turn off the net , now the various activities (retrieving data from database) is blank.
It might be that when the net is turned off, there is no session and so no data is retrieved from database.

When the user registers his mobile and his login credential information is maintained by code
SessionContext.loadStoredCredentials(CredentialsStorageBuilder.StorageType.SHARED_PREFERENCES).
this information is saved somewhere.
Is there anyway that this user login credential is saved in mobile itself so that when there is no net, still session can be created and app runs.

Thanks and Regards,
Amit.
thumbnail
Victor Galan, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Regular Member Posts: 144 Join Date: 6/14/16 Recent Posts
Hi!

What version of the library are you using?

Are you editing library code?
you can just set the remote policy in xml of the screenlet adding offlinePolicy="REMOTE_FIRST" and everything should works emoticon

I hope it helps,

Regards
Amit Sinha, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Junior Member Posts: 43 Join Date: 12/31/14 Recent Posts
Hi Victor,

For developing my app i am using liferay-screens:1.4.0, liferay 6.2 ga4, liferay-android-sdk:7.0.1
i have not changed/edited in library code.
i have created a customise list screenlet for web contents similar to bookmark list screenlet (Creating Android List Screenlets). Simailar to BookmarkListScreenlet of example, in createScreenletView() method of WebContentListScreenlet, i have changed OfflinePolicy.REMOTE_ONLY.ordinal() to OfflinePolicy.REMOTE_FIRST.ordinal()

I have added liferay:offlinePolicy="REMOTE_FIRST" to layout xml file, but then also offline is not working

just asking for better understanding.
First i install the software in real mobile device, then run it first time in online mode, after surfing through all activities, i close the app
and then turn off the net (wifi). now open the app, it become blank after login screenlet.
by offline i understands that if there is no net, still data of my web contents should be shown in different activites and a user should surf through activies having data from database.

Thanks and Regards,
Amit.
Amit Sinha, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Junior Member Posts: 43 Join Date: 12/31/14 Recent Posts
Hi,
My app moves from screenlet login activity to activity containing web content screenlet list.
now when i have added below code in login screenlet.
SessionContext.loadStoredCredentials(CredentialsStorageBuilder.StorageType.SHARED_PREFERENCES);

Now for first time, user logins and surf with internet on.
After that internet is turned off and app is closed.
Now when user restarts app in offline mode, screenlet login page is by passed but it does not show web content list

It gives error as
java.net.ConnectException: failed to connect to /192.168.0.105 (port 8080) after 15000ms: connect failed: ENETUNREACH (Network is unreachable) .
This means there is no internet connection,
Does it mean that web content list cannot be displayed without internet on ??
Now how to display the list with internet turned-off

Thanks and Regards
Amit.
thumbnail
Victor Galan, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Regular Member Posts: 144 Join Date: 6/14/16 Recent Posts
Hi amit

Are you using the library implementation of WebContentList or are you using a custom screenlet?
If you are using the built-in WebContentList, by default it uses REMOTE_ONLY offline policy which means that it only tries to get the content online, not from cache, you can test it using the REMOTE_FIRST or CACHE_FIRST policies to retrieve values from cache

Regarding your first question, can you please share some interactor code from your custom screenlet, cache logic is done there

I hope it helps,

Regards,

Victor
Amit Sinha, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Junior Member Posts: 43 Join Date: 12/31/14 Recent Posts
Hi Victor,
I am using custom screenlet for web content.
Below code is from customise list screenlet for web content (webContentListScreenlet)


public class WebContentListScreenlet extends BaseListScreenlet<webcontent, webcontentlistinteractorimpl>
{ 
  private long _groupId,  _folderId;
  private OfflinePolicy _offlinePolicy;
   ..............    
      @Override
      protected View createScreenletView(Context context, AttributeSet attributes)
      {
            TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributes, R.styleable.WebContentListScreenlet, 0, 0);
        
            /* REMOTE_ONLY is changed to REMOTE_FIRST  */
            Integer offlinePolicy = typedArray.getInteger(R.styleable.WebContentListScreenlet_offlinePolicy, OfflinePolicy.REMOTE_FIRST.ordinal());
        
            _offlinePolicy = OfflinePolicy.values()[offlinePolicy];
            _groupId = typedArray.getInt(R.styleable.WebContentListScreenlet_groupId, (int) LiferayServerContext.getGroupId());
            _folderId = typedArray.getInt(R.styleable.WebContentListScreenlet_folderId, 0);
        
             typedArray.recycle();
	      return super.createScreenletView(context, attributes);
       } 
     ............
}
</webcontent,>


Thanks and Regards,
Amit
thumbnail
Victor Galan, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Regular Member Posts: 144 Join Date: 6/14/16 Recent Posts
Amit Sinha:
Hi Victor,
I am using custom screenlet for web content.
Below code is from customise list screenlet for web content (webContentListScreenlet)


public class WebContentListScreenlet extends BaseListScreenlet<webcontent, webcontentlistinteractorimpl>
{ 
  private long _groupId,  _folderId;
  private OfflinePolicy _offlinePolicy;
   ..............    
      @Override
      protected View createScreenletView(Context context, AttributeSet attributes)
      {
            TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributes, R.styleable.WebContentListScreenlet, 0, 0);
        
            /* REMOTE_ONLY is changed to REMOTE_FIRST  */
            Integer offlinePolicy = typedArray.getInteger(R.styleable.WebContentListScreenlet_offlinePolicy, OfflinePolicy.REMOTE_FIRST.ordinal());
        
            _offlinePolicy = OfflinePolicy.values()[offlinePolicy];
            _groupId = typedArray.getInt(R.styleable.WebContentListScreenlet_groupId, (int) LiferayServerContext.getGroupId());
            _folderId = typedArray.getInt(R.styleable.WebContentListScreenlet_folderId, 0);
        
             typedArray.recycle();
	      return super.createScreenletView(context, attributes);
       } 
     ............
}
</webcontent,>


Thanks and Regards,
Amit



Hi!

Yeah but can you share interactor code? instead of screenlet code?

You cannot use our built-in WebContentListScreenlet?

Regards,
Amit Sinha, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Junior Member Posts: 43 Join Date: 12/31/14 Recent Posts
Hi Victor,

I have to display list of web contents from one folder and those webcontents are dynamic and could be removed.
i do not have to write hardcoded web content id in layout.
that's why i have created webcontent list screenlet.

Are you asking for class ListInteractorImpl or interface Interactor
i have not written code for interface Interactor.

For creating customise List Screenlet for Web content, i followed below url
https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/creating-android-list-screenlets.

Following the url, similar to class BookmarkListInteractorImpl, i created class WebContentListInteractorImpl.
Similar to offline methods of class BookmarkListInteractorImpl, i have written following code


public class WebContentListInteractorImpl  extends BaseListInteractor<webcontent, baselistinteractorlistener<webcontent>&gt;
{
	.....
	public WebContentListInteractorImpl(int targetScreenletId, OfflinePolicy offlinePolicy)
    {
        super(targetScreenletId, offlinePolicy);
    }
	
	private enum WebContent_LIST implements CachedType
    {
        WebContent, WebContent_COUNT
    }
	
	@NonNull
    @Override
    protected WebContent getElement(TableCache tableCache) throws JSONException
    {
        return new WebContent(JSONUtil.toMap(new JSONObject(tableCache.getContent())));
    }

	@Override
    protected String getContent(WebContent object)
    {
        // return new JSONObject(object.getValues()).toString();
        String strOfflineContent="";
        try
        {
               //  strOfflineContent will contain  list content
                strOfflineContent =  new JSONObject(object.getStrContent()).toString();
        }
        catch (JSONException e)
        {
                e.printStackTrace();
        }
		return offlineContent;
    }
	
	@Override
    protected void storeToCache(BaseListEvent event, Object... args)
    {
        storeRows(String.valueOf(_folderId), WebContent_LIST.WebContent, 
                WebContent_LIST.WebContent_COUNT, _groupId, null, event);
    }
	
	@Override
    protected boolean cached(Object... args) throws Exception
    {
        final int startRow = (int) args[0];
        final int endRow = (int) args[1];
        final Locale locale = (Locale) args[2];

        return recoverRows(String.valueOf(_folderId), WebContent_LIST.WebContent,
                WebContent_LIST.WebContent_COUNT, _groupId, null, locale, startRow, endRow);
    }
	.....
}
</webcontent,>


My code is working fine in online mode.
I do not have idea about interface interactor

Thanks and Regards,
Amit.
thumbnail
Victor Galan, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Regular Member Posts: 144 Join Date: 6/14/16 Recent Posts
Hi Amit

You can use WebContentListScreenlet which allows you to do exactly what you want.

I just did a quick test and everything was working nice even offline, could you try using this screenlet?

I hope it helps,

REgards,

Victor
Amit Sinha, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Junior Member Posts: 43 Join Date: 12/31/14 Recent Posts
Hello Victor,
I tried with the available Web Content List screenlet which displays the web content list while offline.
But when i try to select any web content from that list, i am unable to select any web content.
below method is not called, when i try to select any web content from the list

public void onListItemSelected(WebContent element, View view){ }

for this activity, i am implementing Interface BaseListListener<WebContent>
What i am missing ?

Can i call two or more FolderId programatically for displaying different web content list
or i have to direct to different activities for different web content list (of different sites)

Thanks and Regards,
Amit.
thumbnail
Victor Galan, modified 7 Years ago.

RE: How to get android screenlet offline mode working for app?

Regular Member Posts: 144 Join Date: 6/14/16 Recent Posts
Amit Sinha:
Hello Victor,
I tried with the available Web Content List screenlet which displays the web content list while offline.
But when i try to select any web content from that list, i am unable to select any web content.
below method is not called, when i try to select any web content from the list

public void onListItemSelected(WebContent element, View view){ }

for this activity, i am implementing Interface BaseListListener<WebContent>
What i am missing ?

Can i call two or more FolderId programatically for displaying different web content list
or i have to direct to different activities for different web content list (of different sites)

Thanks and Regards,
Amit.


Hi Amit

You have to implement WebContentListListener instead, and you have to call yourScreenlet.setListener(this); I just tested it and the method you want is being called.

And if you want to display another folder in the same screenlet you can change the folderId programmatically, and load the screenlet again.


yourScreenlet.setFolderId(yourNewFolderId);
yourScreenlet.loadPage(0);


I hope it helps,
Regards,
VĂ­ctor