Can't connect IExtension object to BrowserHelperObject - javascript

I'm trying to call a BHO plugin function from javascript, that BHO injected to page.
But:
console.log(window.myExtension) is "none" or "undefined"
OnDocumentComplete fires not on each browser start. (it's because of IE?)
OnDocumentComplete not fires on F5, just if set cursor in address field and press Enter (and see 2nd)
Complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using SHDocVw;
using mshtml;
using Microsoft.Win32;
using INISpace;
namespace IEPlugin
{
[ComVisible(true),
Guid("4C1D2E51-018B-4A7C-8A07-618452573E42"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IExtension
{
[DispId(1)]
void alert(string message);
}
// IObjectWithSite GUID
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")]
public interface IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);
}
[ComVisible(true),
Guid("DA8EA345-02AE-434E-82E9-448E3DB7629E"),
ClassInterface(ClassInterfaceType.None), ProgId("MyExtension"),
ComDefaultInterface(typeof(IExtension))]
public class BrowserHelperObject : IObjectWithSite, IExtension
{
private WebBrowser webBrowser;
public void alert(string message) { System.Windows.Forms.MessageBox.Show("BHO: " + message); }
public void OnDocumentComplete(dynamic frame, ref dynamic url)
{
if (!ReferenceEquals(frame, webBrowser))
{
return;
}
dynamic window = webBrowser.Document.parentWindow;
IExpando windowEx = (IExpando)window;
windowEx.AddProperty("myExtension");
//window.myExtension = this; crash that piece of shit
this.alert("frame IS browser" + windowEx);
HTMLDocument document = (HTMLDocument)webBrowser.Document;
IHTMLScriptElement scriptObject = (IHTMLScriptElement)document.createElement("script");
scriptObject.type = #"text/javascript";
scriptObject.text = "console.log(window.myExtension);";
document.appendChild((IHTMLDOMNode)scriptObject);
}
public int SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
webBrowser = null;
}
return 0;
}
public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}
public const string BHO_REGISTRY_KEY_NAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);
if (registryKey == null)
registryKey = Registry.LocalMachine.CreateSubKey(BHO_REGISTRY_KEY_NAME);
string guid = type.GUID.ToString("B");
RegistryKey ourKey = registryKey.OpenSubKey(guid);
if (ourKey == null)
{
ourKey = registryKey.CreateSubKey(guid);
}
ourKey.SetValue("NoExplorer", 1, RegistryValueKind.DWord);
registryKey.Close();
ourKey.Close();
}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);
string guid = type.GUID.ToString("B");
if (registryKey != null)
registryKey.DeleteSubKey(guid, false);
}
}
}

Related

How to pass data from database popup list to javascript interface android and add this value on html webview

popupdialog.java
public void showCustomRegionPopup() {
.
.
.
listRegion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String region = (String) listRegion.getItemAtPosition(position);
SQLiteDatabase database = context.pdfDatabaseManager.getReadableDatabase();
final Cursor c = database.rawQuery("Select * From " + PDFDatabaseManager.DATABASE_REGION_TABLE + " WHERE "+ PDFDatabaseManager.KEY_REGION +"='"+region+"'", null);
String code = "";
while (c.moveToNext()) code = c.getString(1);
c.close();
context.setCodeAndRegion(code,region);
a.dismiss();
}
});
androidaction.java
#JavascriptInterface
public void showCustomRegionPopup(){
context.popupDialog.showCustomRegionPopup();
}
I want to take database region value from popupdialog.java and pass data to androidaction.java and run this code to put them on html...
webview.post(new Runnable() {
#Override
public void run() {
webview.evaluateJavascript("javascript:showCustomRegionPopup('".concat(region).concat("');"), null);
}
});
}

How fix to SMS Retriever API

I wrote code for example "SMS Retreiver API" https://developers.google.com/identity/sms-retriever/request
but I don`t result which I wont
This code past to MainActivity.
SmsRetrieverClient client = SmsRetriever.getClient(this);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
}
});
task.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
This code past to MySMSBroadcastReceiver.
public class MySMSBroadcastReceiver extends BroadcastReceiver {
String message;
Status status;
private static MessageListener mListener;
#Override
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch(status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
// Extract one-time code from the message and complete verification
// by sending the code back to your server.
break;
case CommonStatusCodes.TIMEOUT:
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
break;
}
mListener.MySMSBroadcastReceiver(message);
}
}
public static void bindListener(MessageListener listener){
mListener = listener;
}
}
In my Manifest
<receiver android:name="ru.project.MBank.MySMSBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
But result get nothing.
Help what do I do wrong?
I had same problem. First you need to generate a unique key (App Signature) that will identify message and your device. Once you generate key, your broadcaster will be able to detect message.
public class AppSignature extends ContextWrapper {
public static final String TAG = AppSignature.class.getSimpleName();
private static final String HASH_TYPE = "SHA-256";
public static final int NUM_HASHED_BYTES = 9;
public static final int NUM_BASE64_CHAR = 11;
public AppSignature(Context context) {
super(context);
}
/**
* Get all the app signatures for the current package
* #return
*/
public ArrayList<String> getAppSignatures() {
ArrayList<String> appCodes = new ArrayList<>();
try {
// Get all package signatures for the current package
String packageName = getPackageName();
PackageManager packageManager = getPackageManager();
Signature[] signatures = packageManager.getPackageInfo(packageName,
PackageManager.GET_SIGNATURES).signatures;
// For each signature create a compatible hash
for (Signature signature : signatures) {
String hash = hash(packageName, signature.toCharsString());
if (hash != null) {
appCodes.add(String.format("%s", hash));
}
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Unable to find package to obtain hash.", e);
}
return appCodes;
}
private static String hash(String packageName, String signature) {
String appInfo = packageName + " " + signature;
try {
MessageDigest messageDigest = MessageDigest.getInstance(HASH_TYPE);
messageDigest.update(appInfo.getBytes(StandardCharsets.UTF_8));
byte[] hashSignature = messageDigest.digest();
// truncated into NUM_HASHED_BYTES
hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
// encode into Base64
String base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING | Base64.NO_WRAP);
base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR);
return base64Hash;
} catch (NoSuchAlgorithmException e) {
}
return null;
}
}
After this initiate this class in your firs activity.
Hope this will help you.

Added columns to SQLiteDatabase, can no longer read from it

My SQLiteDatabase was working fine with just 3 entries, the UUID, Title and Date, but ever since I added some more columns I am getting this error.
Not sure what it can be, I have read that 0,-1 means that the column cannot be read, but I have made sure to spell all my column names correctly.
CrimeCursorWrapper.java
public List<Crime> getCrimes() {
List<Crime> crimes = new ArrayList<>();
CrimeCursorWrapper cursor = queryCrimes(null, null);
try {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
crimes.add(cursor.getCrime());
cursor.moveToNext();
}
} finally{
cursor.close();
}
return crimes;
}
CrimeLab.java:
public Crime getCrime(UUID id) {
CrimeCursorWrapper cursor = queryCrimes(
CrimeTable.Cols.UUID + " = ?",
new String[] { id.toString() }
);
try {
if (cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return cursor.getCrime();
} finally {
cursor.close();
}
}
private static ContentValues getContentValues(Crime crime) {
ContentValues values = new ContentValues();
values.put(CrimeTable.Cols.UUID, crime.getId().toString());
values.put(CrimeTable.Cols.TITLE, crime.getTitle());
values.put(CrimeTable.Cols.DATE, crime.getDate().getTime());
values.put(CrimeTable.Cols.ACTTYPE, crime.getActType().toString());
values.put(CrimeTable.Cols.PLACE, crime.getPlace().toString());
values.put(CrimeTable.Cols.DURATION, crime.getDuration().toString());
values.put(CrimeTable.Cols.COMMENT, crime.getComment().toString());
return values;
}
private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
CrimeTable.NAME,
null, // Columns - null selects all columns
whereClause,
whereArgs,
null, // groupBy
null, // having
null // orderBy
);
return new CrimeCursorWrapper(cursor);
}
CrimeCursorWrapper.java:
public class CrimeCursorWrapper extends CursorWrapper{
public CrimeCursorWrapper(Cursor cursor) {
super(cursor);
}
public Crime getCrime() {
String uuidString = getString(getColumnIndex(CrimeTable.Cols.UUID));
String title = getString(getColumnIndex(CrimeTable.Cols.TITLE));
long date = getLong(getColumnIndex(CrimeTable.Cols.DATE));
String actType = getString(getColumnIndex(CrimeTable.Cols.ACTTYPE));
String place = getString(getColumnIndex(CrimeTable.Cols.PLACE));
String duration = getString(getColumnIndex(CrimeTable.Cols.DURATION));
String comment = getString(getColumnIndex(CrimeTable.Cols.COMMENT));
Crime crime = new Crime(UUID.fromString(uuidString));
crime.setTitle(title);
crime.setDate(new Date(date));
crime.setActType(actType);
crime.setPlace(place);
crime.setDuration(duration);
crime.setComment(comment);
return crime;
}
}
Crime.java:
public class Crime {
private UUID mId;
private String mTitle;
private Date mDate;
private String mActType;
private String mPlace;
private String mDuration;
private String mComment;
public Crime() {
this(UUID.randomUUID());
}
public Crime(UUID id) {
mId = id;
mDate = new Date();
}
public UUID getId() {
return mId;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public Date getDate() {
return mDate;
}
public void setDate(Date date) {
mDate = date;
}
public String getPhotoFilename() {
return "IMG_" + getId().toString() + ".jpg";
}
public String getActType() {
return mActType;
}
public void setActType(String actType) {
mActType = actType;
}
public String getPlace() {
return mPlace;
}
public void setPlace(String place) {
mPlace = place;
}
public String getDuration() {
return mDuration;
}
public void setDuration(String duration) {
mDuration = duration;
}
public String getComment() {
return mComment;
}
public void setComment(String comment) {
mComment = comment;
}
}
It "CAN" be the reason: If you use your physical mobile device for debugging, after you change your database tables inside the code, delete your application's data from the mobile device and reinstall your apk (or press debug or run buttons). Database files are not updating automatically by Android Studio.
Did you change version of your database after adding more columns? It is mandatory.

p:commandButton execution order of events

I am using PrimeFaces 6.0 components:
<p:commandButton type="submit" value="Create Customer"
icon="ui-icon-check"
actionListener="#{newCustomerBean.saveNewCustomer}"
update = "#form"
oncomplete="ajaxUploadFile();"/>
<p:inputText id="saveCustomerId" value ="#{newCustomerBean.savedKundeId}"/>
and I want to execute the following sequence of actions with them:
1.) Execute the actionListener method on the backing bean to save a customer;
2.) Update the form field saveCustomerId with the id of the customer that is saved on step (1). The actionListener method generates a customer Id after the successful save and stores is as a bean property;
3.) Execute the Java Script method ajaxUploadFile()
According to the link
Execution order of events when pressing PrimeFaces p:commandButton
this sequence shall be as I have imagined.
However, in reality, the method
ajaxUploadFile()
is called BEFORE the input field with id saveCustomerId is updated.
Could you help me get the right sequence?
Here is the backing bean:
#ManagedBean
#ViewScoped
public class NewCustomerBean implements Serializable {
public enum KundeTyp {
TYP_NATPERS("Nat. Person"), TYP_FIRMA("Firma");
private String value;
private KundeTyp(String value) {
this.value = value;
}
#Override
public String toString() {
return value;
}
}
private KundeTyp custmerType;
private Map<String, KundeTyp> custmerTypes;
private long savedKundeId;
#Inject
private KundeDBService kundeService;
private String vorname;
private String addresse;
private String steuerNummer;
private String kundeTyp = Integer.MIN_VALUE + "";
#PostConstruct
public void init() {
custmerTypes = new HashMap<String, KundeTyp>();
custmerTypes.put(KundeTyp.TYP_NATPERS.value, KundeTyp.TYP_NATPERS);
custmerTypes.put(KundeTyp.TYP_FIRMA.value, KundeTyp.TYP_FIRMA);
}
public KundeTyp getCustmerType() {
return custmerType;
}
public void setCustmerType(KundeTyp custmerType) {
this.custmerType = custmerType;
}
public Map<String, KundeTyp> getCustmerTypes() {
return custmerTypes;
}
public void setCustmerTypes(Map<String, KundeTyp> custmerTypes) {
this.custmerTypes = custmerTypes;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getAddresse() {
return addresse;
}
public void setAddresse(String addresse) {
this.addresse = addresse;
}
public String getSteuerNummer() {
return steuerNummer;
}
public void setSteuerNummer(String steuerNummer) {
this.steuerNummer = steuerNummer;
}
public String getKundeTyp() {
return kundeTyp;
}
public void setKundeTyp(String kundenTyp) {
this.kundeTyp = kundenTyp;
}
public String saveNewCustomer(ActionEvent e) {
Kunde neuerKunde = null;
switch (this.custmerType) {
case TYP_NATPERS: {
neuerKunde = new NatuerlichePerson();
break;
}
case TYP_FIRMA: {
neuerKunde = new Firma();
((Firma) neuerKunde).setSteuerNummer("123456");
break;
}
}
neuerKunde.setVorname(vorname);
neuerKunde.setAdresse(this.addresse);
try {
savedKundeId = kundeService.saveKunde(neuerKunde);
} catch (ServiceException se) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
"Unable to save the new customer: " + se.getMessage()));
}
return null;
}
public long getSavedKundeId() {
return savedKundeId;
}
public void setSavedKundeId(long savedKundeId) {
this.savedKundeId = savedKundeId;
}
}
I would propose a work-around here, since I was not able to find a solution.
Instead of updating the customerId on the front-end, we put it as a session attribute in the HttpSession.
Then, in the UploadServlet, which handles the file upload, we read this attribute and save the image under this customerId.

Android WebView utilizing Camera and GPS

I'm playing around with the WebView in the Android browser, but is wondering if anyone have used the browser together with the html5 to use the camera and gps of the local device?
Or will I need to do a Javascript connection to the Java source code for this?
Will this also work in IOS?
This is, without using PhoneGap.
best,
Henrik
It might be a bit late, but just in case I will give you an example to do that.
(Camera)
1////add this to your webChromeClient
myWebView.setWebChromeClient(new WebChromeClient()
{
public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType )
{
mUploadMessage = uploadMsg;
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
File photo = new File(Environment.getExternalStorageDirectory(), sdf.format(cal.getTime()) +".jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
picUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, TAKE_PICTURE);
}
});
2///add this function
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case TAKE_PICTURE:
if(resultCode == Activity.RESULT_OK)
{
Uri mypic = picUri;
mUploadMessage.onReceiveValue(mypic);
mUploadMessage = null;
}
else
{
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
return;
}
default:
{
return;
}
}
}
3// and the HTML looks like this
<input type="file" id="files" name="files" accept="image/*" capture="camera" >
The above code will open the native camera app.
P.S. This is a mix of code from different sources.
It's worth pointing out where the code shown here should live, as it's not obvious for a newbie - it goes in your Activity class, i.e.
public class MyActivity extends Activity {
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
}
#Override
public void onCreate(Bundle savedInstanceState) {
...
myWebView.setWebChromeClient(new WebChromeClient()
{
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
...
}
});
}
}
In my case, I've override the makeWebViewEngine method in order to be able to modify the CordovaWebViewEngine casted to a SystemWebViewEngine as follows (based on this answer):
public class MainActivity extends CordovaActivity {
private static final int FILECHOOSER_RESULTCODE = 12345;
private ValueCallback<Uri> mUploadMessage;
private Uri mPicUri;
private ValueCallback<Uri[]> mFilePathCallback;
private String mCameraPhotoPath;
#Override
protected CordovaWebViewEngine makeWebViewEngine() {
SystemWebViewEngine systemWebViewEngine = (SystemWebViewEngine) super.makeWebViewEngine();
SystemWebView systemWebView = (SystemWebView) systemWebViewEngine.getView();
systemWebView.setWebChromeClient(new SystemWebChromeClient(systemWebViewEngine) {
// For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
File photo = new File(Environment.getExternalStorageDirectory(), sdf.format(cal.getTime()) + ".jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
mPicUri = Uri.fromFile(photo);
startActivityForResult(cameraIntent, FILECHOOSER_RESULTCODE);
}
// For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
// Set up the take picture intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
// Set up the intent to get an existing image
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
// Set up the intents for the Intent chooser
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, MainActivity.FILECHOOSER_RESULTCODE);
return true;
}
});
return systemWebViewEngine;
}
// …
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}
Furthermore, based on this other answer, I've had to specify the openFileChooser method for Android 4.1 and, for Android 5.0+, I've had to specify the onShowFileChooser method.
By the way, I've modified the onActivityResult method as follows:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (Build.VERSION.SDK_INT >= 21) {
if (requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
} else {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} else {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result;
if (resultCode != RESULT_OK) {
result = null;
} else {
result = intent == null ? this.mPicUri : intent.getData(); // retrieve from the private variable if the intent is null
}
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;
}
}
}
Thanks to #Will and #Christian for pointing the good direction in this answer BTW :)

Categories