Embed zxing barcode scanner to the activity - android

I'm writing android application and my client requires a barcode scanner in it. They are really specific about it, so the layout they want is like this:
If a qr code found - it jumps to another window automatically. If manual pressed - you are asked to type manually and proceed with the rest of the app.
So basically I could embed zxing code to my app and add it to the activity but I don't want that and would like to have it as a separate app.
What I have at the moment is a separate activity called like this:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
I also tried this:
IntentIntegrator intentIntegrator = new IntentIntegrator(this);
Intent i = intentIntegrator.initiateCustomScan();
LocalActivityManager mgr = getLocalActivityManager();
Window w = mgr.startActivity("unique_per_activity_string", i);
View wd = w != null ? w.getDecorView() : null;
if(wd != null) {
scanButton.addView(wd);
}
But then I get java.lang.SecurityException:
03-19 12:22:55.890: E/AndroidRuntime(29394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.menucard.barcode.scan/com.barcode.scan.ScanActivity}: java.lang.SecurityException: Requesting code from com.google.zxing.client.android (with uid 10139) to be run in process com.menucard.barcode.scan (with uid 10169)
Maybe someone has an idea how to add a separate app into my activity? Or other ways to accomplish this?

You can't embed an external app in another app via Intent unfortunately. The external app here needs to take over the whole screen, and is in landscape mode, for starters.
You should write your own app, but can reuse parts of Barcode Scanner in your app so that it's not entirely from scratch. Just please don't copy the AndroidManifest.xml file. I think it will also be clearly not confused with Barcode Scanner given the different UI. All that remains is to make sure you follow the terms of the Apache License (easy).

#MindaugasSvirskas, your last comment is exactly what I was about to post now:-) I have faced the same problem in the past, in several apps, and believe me, just make use of Intents, that's the way the whole Android system is designed, favouring intercommunication between apps. iOS programmers can easily integrate the scanning Zxing layout in their own layouts, but we are supposed to make use of intents, and I agree.

Related

How to open Android Outlook application from an external one

I'm currently developing an Android application in order to display home screen widgets. Those ones are related to Microsoft Outlook (Events + Messages) in order to show incoming events and unread new messages in a kind of dynamic tiles.
The Msal graph library helps me a lot to authenticate and retrieve in formations which contains an identifier for each event / message results
But now I want to know if the outlook application is installed on the user device and if there is a way to open Outlook when the user click on the widget. Moreover if the user can open the corresponding clicked event or message with the identifier.
For example the Event widget currently displaying a birthday event. The user click on it. Then it opens Outlook and display directly that birthday event.
Regards
I don't think this is officially documented somewhere. But here's what you can do to find out about it.
You can list all Microsoft applications installed on your device...
val packages = context.packageManager
.getInstalledApplications(PackageManager.GET_META_DATA)
for (info in packages) {
if(info.packageName.startsWith("com.microsoft", true)){
Log.d("package name:" + info.packageName)
Log.d("Launch Activity: " + context.packageManager.getLaunchIntentForPackage(info.packageName))
}
}
Take a note of the "launch intent" displayed in the LogCat. You can use that to launch Outlook. Just make sure you don't hard-code those values because Microsoft can change those values at any point, for example the activity class can change. So, instead of doing this...
context.startActivity(
Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
setPackage("com.microsoft.office.outlook")
component = ComponentName("com.microsoft.office.outlook", "com.microsoft.office.outlook.MainActivity")
}
)
Do this...
context.startActivity(
Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName(
outlookLaunchIntent?.component?.packageName,
outlookLaunchIntent?.component?.className
)
setPackage(outlookLaunchIntent.package)
}
)
Also, remember that getLaunchIntentForPackage and component can return null, so make sure you check for null values properly
I am relaying a suggestion from a couple of internal folks:
Please try to open the event using one of the following URLs:
ms-outlook://events/open?restid=%s&account=test#om.com (if you have a regular REST id)
ms-outlook://events/open?immutableid=%s&account=test#om.com (if you are using an immutable id)
Since immutable IDs are still in preview stage in Microsoft Graph, and customers should not use preview APIs in their production apps, I think option #1 applies to your case.
Please reply here if the URL works, or not, and if you have other related questions. I requested the couple of folks to keep an eye on this thread as well.
Well, i managed to open the outlook android application with the help of your code #Leo. As im not developping with Kotlin, ill post the JAVA code below :
Intent outlookLaunchIntent = context.getPackageManager().getLaunchIntentForPackage("com.microsoft.office.outlook");
if (outlookLaunchIntent != null) {
context.startActivity(outlookLaunchIntent );
}
Below code to open event/message in a web browser provided by webLink property of the graph API. (I only test for event and the url provided not working. Ill post a new issue on StackOverFlow for that but you already see the issue over there : https://github.com/microsoftgraph/microsoft-graph-docs/issues/4203
try {
Intent webIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(calendarWebLink));
webIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(webIntent);
} catch (RuntimeException e) {
// The url is invalid, maybe missing http://
e.printStackTrace();
}
However im still stuck on the decicive goal of my widget item click which is to open the relative event/email in the Microsoft Outlook Android application.
Microsoft Outlook Android app contains widgets which can achieve what im looking for. So i wonder if it is possible to list its broadcast receivers.
The best thing i found is an old manifest for that app but it doesnt help me.
https://gist.github.com/RyPope/df0e61f477af4b73865cd72bdaa7d8c2
Hi may you try to open the event using one of the url:
ms-outlook://events/open?restid=%s&account=test#om.com (If the
user is having rest id)
ms-outlook://events/open?immutableid=%s&account=test#om.com (If
the user is having immutable id)

OnProvideAssistDataListener example

Can someone please provide an example for a real case where I might need to use OnProvideAssistDataListener. I can't seem to wrap my head around it. I look at the source code, and then I look online. Someone online says
Application.OnProvideAssistDataListener allows to place into the
bundle anything you would like to appear in the
Intent.EXTRA_ASSIST_CONTEXT part of the assist Intent
I have also been reading through the Intent Docs.
There is an Now On Tap functionality implemented by Google. By long pressing the Home Button, you will get some  information displayed on the screen.  The information you get depends on what you're viewing on your screen at that time. (for eg: Music app displays information about music on the screen).
To provide additional information to the assistant, your app provides global application context by registering an app listener using registerOnProvideAssistDataListener() and supplies activity-specific information with activity callbacks by overriding onProvideAssistData() and onProvideAssistContent(). 
Now when the user activates the assistant, onProvideAssistData() is called to build a full ACTION_ASSIST Intent with all of the context of the current application represented as an instance of the AssistStructure. You can override this method to place anything you like into the bundle to appear in the EXTRA_ASSIST_CONTEXT part of the assist intent.
In the example below, a music app provides structured data to describe the music album that the user is currently viewing:
#Override
public void onProvideAssistContent(AssistContent assistContent) {
super.onProvideAssistContent(assistContent);
String structuredJson = new JSONObject()
.put("#type", "MusicRecording")
.put("#id", "https://example.com/music/recording")
.put("name", "Album Title")
.toString();
assistContent.setStructuredData(structuredJson);
}
For more info refer https://developer.android.com/training/articles/assistant.html

How to achieve interaction between Native and Hybrid Applications in Worklight?

I will start with explaining the use case I am trying to implement. I have two different applications:
Native android application, and
Worklight-based hybrid application
The use case starts with opening native android application. On a particular event I am opening the Hybrid application with some parameters.
In the Hybrid application, I am getting the passed parameters in the native side it, and now I want to use that data in the webview of the application (JavaScript, HTML). How can I achieve that?
For example:
I opened the first android application. Which have one text box and a button. I entered my mobile number in text box and hit the button. On the button click I have code which starts the other hybrid application and passes the mobile number with it.
I am able to extract that mobile number parameter on native side of code. How to pass that to the Web (JavaScript) part of it?
Any help will be appreciated.
If you are using Worklight 6.2, you can achieve this in 2 ways.
Use the Simple Data Sharing API
With this API I don't think you'll even need to try to get the data from the native view and move it back to the webview in your Hybrid app, it will just be available in the webview.
Explaining the concept and execution in this answer will make it too long; I suggest to first review the documentation and see whether it fits your needs.
But I suggest:
Use the Action Sender API
With this API you can easily move data from web to native or native to web.
In your case, you say you already have the data in the native code after opening the Hybrid application, and you only need to move it to the webview, so what that is required is to:
Add a sender in the native code
Add a receiver in the JavaScript code
Unfortunately at this time there is no training module available to demonstrate specifically this feature, but there will be.
This is the basic premise for what you'll need to do:
In the JavaScript you implement a receiver:
function wlCommonInit(){
WL.App.addActionReceiver ("doSomething", actionReceiver);
}
function actionReceiver(received){
// Do something with the received data.
alert (received.data.someProperty);
}
In the main Java class of the Hybrid application (or elsewhere, depending on your application) you implement the following in onInitWebFrameworkComplete after the else closing bracket:
public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result){
...
...
else {
handleWebFrameworkInitFailure(result);
}
JSONObject data = new JSONObject();
try {
data.put("someProperty", 12345);
} catch (JSONException e) {
// handle it...
}
WL.getInstance().sendActionToJS("doSomething", data);
}
The end result would be that once you open the app, you'll be welcomed with an alert showing "12345".
I will describe the solution using code snippets.
First Open the hybrid application from a native application.
Intent intent = getPackageManager().getLaunchIntentForPackage(“URI Of Target Application”);
intent.putExtra("someData", someData);
startActivity(intent);
Now Worklight based hybrid application will start and from native part we will extract that passed data and store it in shared preferences:
Bundle dataBundle = getIntent().getExtras();
String someData = dataBundle.getString("someData");
sharedpreferences = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
sharedpreferences.edit().putString("someData", someData);
sharedpreferences.commit();
Now make a plugin which you can call after the web part is ready for use.
SharedPreferences sharedpreferences = cordova.getActivity().getSharedPreferences(MyPREFERENCES,cordova.getActivity().MODE_PRIVATE);
if(sharedpreferences!=null) {
String param = sharedpreferences.getString("someData", "-1");
sharedpreferences.edit().remove("someData").commit();
callbackContext.success(param);
}
Call that plugin on web side of Worklight based hybrid application.
function onSuccessSharedData (param) {
Param is the passed parameter
}
Cordova.exec(onSuccessSharedData, onFailure, "pluginName", "action", []);

Zxing - My app does not want to start the camera

first of all i will introduce myself. I am a young student from austria who is new to Android programming. My Project is to write a Barcode scanner and if you scan a product you get more information about it.
So lets get started with the real problem now:
I have done everything what the tutorials say and it works. The ZXING-Source Code is now a libary for my own Project and ofcourse it is included.
When i export the Android project as a FILENAME.APK and copy it to my SGS3 everything works.
Then i install the apk on my phone and start running the app. When i press the scan barcode button which calls the "onClick"-Method it does nothing. Android says to me the application has ben stopped.
I dont know which function i have to use. Should i use the IntentIntegrator methods or the intent methods to be able to scan a code. ( I know i cant use both in my Method, pasted it here to show your my problem )
public void onClick(final View view)
{
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
this integration is provided by intents,so you had to have installed app which runs with specific intent (action=com.google.zxing.client.android.SCAN) like Barcode Scanner
i'm pretty sure that you don't have such app on your other phone and thats why you're getting "activity not found" Exception now
Edit: about diffs between IntentIntegrator and plain Intent ... well, there is no diffs beside that with IntentIntegrator you have it all nicely wrapped

Embed 3th party activity in my Activity

I'm using the following code to get the activity of a 3th party application and to put it in my activity:
LocalActivityManager mgr = getLocalActivityManager();
Intent i = new Intent(this, SomeActivity.class);
Window w = mgr.startActivity("unique_per_activity_string", i);
View wd = w != null ? w.getDecorView() : null;
if(wd != null) {
mSomeContainer.addView(wd);
}
Copyright Synic: android: using ActivityGroup to embed activities
However, due Security Restrictions, I'm receiving the following error:
java.lang.SecurityException: Requesting code from com.google.android.youtube (with uid 10065) to be run in process com.xxx.xxx (with uid 10144). (It is possible to show your own activity with your own SharedUID from your own application.)
Now i'm wondering if there is any way I can avoid this. By using rooted devices(?), bug in Android OS, or anything else. If I can get it to work by rooting my device, how would i achieve it? (not the rooting ofc)
I'm using the following code to get the activity of a 3th party application and to put it in my activity
That is not supported, sorry.
Now i'm wondering if there is any way I can avoid this.
You are welcome to grab the source code to Android, modify it to suit, put your altered OS into your own modded ROM, and install that ROM mod on whatever devices you are able to.

Categories

Resources