-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathContextExt.kt
324 lines (289 loc) · 9.07 KB
/
ContextExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* Extentions context level */
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import timber.log.Timber
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.util.*
import android.annotation.SuppressLint
import android.os.Build
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.PopupMenu
import android.view.View
/**
* Using it for making your status bar is transparent
*
* @param decorView => your global view in activity
*/
@SuppressLint("ObsoleteSdkInt")
fun Context.transparentStatusBar(
decorView: View
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
}
//=========== How to using it ===========
//override fun onCreate(savedInstanceState: Bundle?) {
// super.onCreate(savedInstanceState)
// setContentView(resLayout)
// transparentStatusBar(window.decorView)
//}
//=======================================
}
/**
* Using it for show icon inside popup menu
*
* @param popupMenu => your popup menu
*/
fun Context.setForceShowIcon(popupMenu: PopupMenu) {
try {
val fields = popupMenu.javaClass.declaredFields
for (field in fields) {
if ("mPopup" == field.name) {
field.isAccessible = true
val menuPopupHelper = field.get(popupMenu)
val classPopupHelper = Class.forName(menuPopupHelper.javaClass.name)
val setForceIcons = classPopupHelper.getMethod(
"setForceShowIcon", Boolean::class.javaPrimitiveType
)
setForceIcons.invoke(menuPopupHelper, true)
break
}
}
} catch (e: Throwable) {
e.printStackTrace()
}
//=========== How to using it ===========
// popUp.apply {
// dismiss()
// show()
// setForceShowIcon(this)
// }
//=======================================
}
/**
* Using it for move to another page with clearing existing activity stack
*/
inline fun <reified T : AppCompatActivity> Context.navigatorWithActivityClearTop() {
val intent = Intent(this, T::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
//=========== How to using it ===========
// navigatorWithActivityClearTop<YourActivity>()
//=======================================
}
/**
* Using it for move to another page without param
*/
inline fun <reified T : AppCompatActivity> Context.navigator(
) {
val intent = Intent(this, T::class.java)
startActivity(intent)
//=========== How to using it ===========
// navigator<YourActivity>()
//=======================================
}
/**
* Using it for move to another page with a single param
* If you want sand data using object, model, etc convert it into string using Gson
*
* @param param => param with string type
*/
inline fun <reified T : AppCompatActivity> Context.navigator(
intentParams : Intent.() -> Unit
) {
val intent = Intent(this, T::class.java)
intent.intentParams()
startActivity(intent)
//=========== How to using it ===========
// navigator<DetailActivity> {
// putExtra("KEY1" , "VALUE1")
// putExtra("KEY2" , "VALUE2")
// }
//=======================================
}
/**
* Using it for moving to another page with activity package name (usually modular package) without params
*
* @param activityPackage => exp : id.co.gits.feature_home_detail.HomeDetailActivity
*/
fun Context.navigatorImplicit(
activityPackage: String
) {
val intent = Intent()
try {
intent.setClass(
this,
Class.forName(activityPackage)
)
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
//=========== How to using it ===========
// navigatorImplicit(yourActivityPackageName)
//=======================================
}
/**
* Using it for moving to another page with activity package name (usually modular package) with params
*
* @param activityPackage => exp : id.co.gits.feature_home_detail.HomeDetailActivity
*/
fun Context.navigatorImplicit(
activityPackage: String,
intentParams : Intent.() -> Unit
) {
val intent = Intent()
try {
intent.intentParams()
intent.setClass(
this,
Class.forName(activityPackage)
)
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
//=========== How to using it ===========
// navigatorImplicit(yourActivityPackageName) {
// putExtra("KEY1" , "VALUE1")
// putExtra("KEY2" , "VALUE2")
// }
//=======================================
}
fun Context.showToast(
message: String
) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
/**
* Using it for loggin debug data using Timber
*
* @param TAG => your TAG usually the name of activity class
* @param message => text info
*/
fun Context.logD(
TAG: AppCompatActivity,
message: String
) {
Timber.d("${TAG::class.java.simpleName} $message")
//=========== How to using it ===========
// logD(YourActivity(), yourMessage)
//=======================================
}
/**
* Using it for logging verbose data using Timber
*
* @param TAG => your TAG usually the name of activity class
* @param message => text info
*/
fun Context.logV(
TAG: AppCompatActivity,
message: String
) {
Timber.v("${TAG::class.java.simpleName} $message")
//=========== How to using it ===========
// logV(YourActivity(), yourMessage)
//=======================================
}
/**
* Using it for logging error data using Timber
*
* @param TAG => your TAG usually the name of activity class
* @param message => text info
*/
fun Context.logE(
TAG: AppCompatActivity,
message: String
) {
Timber.e("${TAG::class.java.simpleName} $message")
//=========== How to using it ===========
// logE(YourActivity(), yourMessage)
//=======================================
}
/**
* Using it for saving bitmap from imageview to image file
*
* @param context => state app
* @param imageBitmap => bitmap from your imageview
* @param directoryName => for naming your directory for saving image view
* @param showMessageStatus => message status if you failed or success creating image file
*/
@SuppressLint("ObsoleteSdkInt")
fun Context.saveBitmapToLocalFile(
context: Context,
imageBitmap: Bitmap,
directoryName: String?,
showMessageStatus: Boolean
) {
val root = Environment.getExternalStorageDirectory().toString()
val directoryNameDefault = if (TextUtils.isEmpty(directoryName)) {
Const.APP_FOLDER_DEFAULT
} else {
directoryName
}
val myDir = File("$root/$directoryNameDefault")
if (!myDir.exists()) {
myDir.mkdirs()
}
val random = Random()
val numbers = 100
val numberResult = random.nextInt(numbers)
val imageFileName = "IMG_$numberResult.png"
val existImageFile = File(myDir, imageFileName)
val outStream: FileOutputStream
val bitmap = imageBitmap
var isSuccessFileSaving: Boolean
try {
outStream = FileOutputStream(existImageFile)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)
/* 100 to keep full quality of the image */
outStream.flush()
outStream.close()
isSuccessFileSaving = true
} catch (e: FileNotFoundException) {
e.printStackTrace()
isSuccessFileSaving = false
} catch (e: IOException) {
e.printStackTrace()
isSuccessFileSaving = false
}
val message = if (isSuccessFileSaving) {
Const.MESSAGE_SUCCESS_IMAGE_SAVE
} else {
Const.MESSAGE_FAILED_IMAGE_SAVE
}
if (showMessageStatus) showToast(context, message)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
val scanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
val contentUri = Uri.fromFile(existImageFile)
scanIntent.data = contentUri
context.sendBroadcast(scanIntent)
} else {
context.sendBroadcast(
Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse(
Const.SDCARD_URI_PATH + Environment
.getExternalStorageDirectory()
)
)
)
}
//=========== How to using it ===========
// saveBitmapToLocalFile(activityContenxt, yourBitmap, dirName, yourBooleanStatus)
//=======================================
}