-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a08b06
commit 27e7e20
Showing
14 changed files
with
194 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
app/src/main/kotlin/com/litekite/sample/appwidget/collection/CollectionWidgetRVFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2021 LiteKite Startup. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.litekite.sample.appwidget.collection | ||
|
||
import android.appwidget.AppWidgetManager | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.RemoteViews | ||
import android.widget.RemoteViewsService | ||
import com.litekite.sample.R | ||
|
||
/** | ||
* @author Vignesh S | ||
* @version 1.0, 28/11/2021 | ||
* @since 1.0 | ||
*/ | ||
class CollectionWidgetRVFactory( | ||
private val context: Context, | ||
intent: Intent | ||
) : RemoteViewsService.RemoteViewsFactory { | ||
|
||
companion object { | ||
private val TAG = CollectionWidgetRVFactory::class.java.simpleName | ||
private const val REMOTE_VIEW_COUNT: Int = 10 | ||
} | ||
|
||
private lateinit var collectionWidgetItems: MutableList<CollectionWidgetItem> | ||
private val appWidgetId: Int = intent.getIntExtra( | ||
AppWidgetManager.EXTRA_APPWIDGET_ID, | ||
AppWidgetManager.INVALID_APPWIDGET_ID | ||
) | ||
|
||
override fun onCreate() { | ||
// In onCreate() you setup any connections / cursors to your data source. Heavy lifting, | ||
// for example downloading or creating content etc, should be deferred to onDataSetChanged() | ||
// or getViewAt(). Taking more than 20 seconds in this call will result in an ANR. | ||
collectionWidgetItems = MutableList(REMOTE_VIEW_COUNT) { index -> | ||
CollectionWidgetItem("$index!") | ||
} | ||
Log.d(TAG, "onCreate: appWidgetId: $appWidgetId") | ||
} | ||
|
||
/** | ||
* Can do intense work | ||
*/ | ||
override fun onDataSetChanged() { | ||
// This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged | ||
// on the collection view corresponding to this factory. You can do heaving lifting in | ||
// here, synchronously. For example, if you need to process an image, fetch something | ||
// from the network, etc., it is ok to do it here, synchronously. The widget will remain | ||
// in its current state while work is being done here, so you don't need to worry about | ||
// locking up the widget. | ||
} | ||
|
||
override fun onDestroy() { | ||
collectionWidgetItems.clear() | ||
} | ||
|
||
override fun getCount(): Int = collectionWidgetItems.size | ||
|
||
/** | ||
* Can do intense work | ||
*/ | ||
override fun getViewAt(position: Int): RemoteViews { | ||
// Construct a remote views item based on the app widget item XML file, | ||
// and set the text based on the position. | ||
return RemoteViews(context.packageName, R.layout.adapter_collection_widget_item).apply { | ||
setTextViewText(R.id.widget_item, collectionWidgetItems[position].text) | ||
|
||
// Next, set a fill-intent, which will be used to fill in the pending intent template | ||
// that is set on the collection view in StackWidgetProvider. | ||
val fillInIntent = Intent().apply { | ||
Bundle().also { extras -> | ||
extras.putInt(CollectionWidgetProvider.EXTRA_ITEM, position) | ||
putExtras(extras) | ||
} | ||
} | ||
// Make it possible to distinguish the individual on-click | ||
// action of a given item | ||
setOnClickFillInIntent(R.id.widget_item, fillInIntent) | ||
} | ||
} | ||
|
||
override fun getLoadingView(): RemoteViews? { | ||
// You can create a custom loading view (for instance when getViewAt() is slow.) If you | ||
// return null here, you will get the default loading view. | ||
return null | ||
} | ||
|
||
override fun getViewTypeCount(): Int = 1 | ||
|
||
override fun getItemId(position: Int): Long = position.toLong() | ||
|
||
override fun hasStableIds(): Boolean = true | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/kotlin/com/litekite/sample/appwidget/collection/CollectionWidgetRVService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2021 LiteKite Startup. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.litekite.sample.appwidget.collection | ||
|
||
import android.content.Intent | ||
import android.widget.RemoteViewsService | ||
|
||
/** | ||
* @author Vignesh S | ||
* @version 1.0, 04/11/2021 | ||
* @since 1.0 | ||
*/ | ||
class CollectionWidgetRVService : RemoteViewsService() { | ||
|
||
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { | ||
return CollectionWidgetRVFactory(this.applicationContext, intent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.