How to initiate a call from Android Wear on notification action - wear-os

I have the following code but its not opening the dialer.
Intent callIntent = new Intent(Intent.ACTION_CALL);
String uri = "tel: " + requestDetail.driver.phone_number.trim();
callIntent.setData(Uri.parse(uri));
PendingIntent callPendingIntent =
PendingIntent.getActivity(context, 0, callIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.uber_badge)
.setVibrate(new long[]{500, 300, 500, 300, 500, 300, 500, 300})
.setContentTitle(title)
.setAutoCancel(false)
.setContentText("Driver Accepted\nETA: " + requestDetail.eta + " mins")
.extend(new NotificationCompat.WearableExtender().setContentIcon(R.drawable.uber_badge))
.extend(new NotificationCompat.WearableExtender().setBackground(bitmap))
.addAction(R.drawable.phone_call_image, null, callPendingIntent)
.setLargeIcon(bitmap);
mNotificationManager.notify(notifyId, notificationBuilder.build());

Persmissions were missing in the AndroidManifest.xml
We need to add the CALL_PHONE permission in both wearable app as well as phone app.
<uses-permission android:name="android.permission.CALL_PHONE"/>

Related

How to display UIDatePicker to choose date

I am following this documentation so that I can pick a date in my Xamarin.iOS app like in this image:
https://learn.microsoft.com/en-us/xamarin/ios/user-interface/controls/picker-images/image9.png
But I can't get it to work. My code:
var datePickerView = new UIDatePicker();
datePickerView.MinimumDate = (NSDate)DateTime.Today.AddDays(-7);
datePickerView.MaximumDate = NSDate.Now;
datePickerView.Mode = UIDatePickerMode.Date;
datePickerView.Locale = NSLocale.FromLocaleIdentifier("en_US");
datePickerView.MoveAndResizeFrame(
UIScreen.MainScreen.Bounds.X - UIScreen.MainScreen.Bounds.Width,
UIScreen.MainScreen.Bounds.Height - 230,
UIScreen.MainScreen.Bounds.Width,
180
);
// Add(datePickerView); do I add it to my view?
I also tried making it my InputView of a UITextField but I get an ugly dialog that's not even usable like this.
var t = new UITextField();
t.InputView = datePickerView;
t.ResizeFrame(300, 50);
t.Placeholder = "enter date";
t.TextColor = UIColor.Black;
Add(t);
I am running this app on iPad with iOS 14.5
The problem is related with the position(frame) you set on the datepicker,the view would locate beyond of the screen in that scenario .
Use the following code , it works fine on my side .
Click on the label , then display the datepicker menu.
var datePickerView = new UIDatePicker();
datePickerView.MinimumDate = (NSDate)DateTime.Today.AddDays(-7);
datePickerView.MaximumDate = NSDate.Now;
datePickerView.Mode = UIDatePickerMode.Date;
datePickerView.Locale = NSLocale.FromLocaleIdentifier("en_US");
datePickerView.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 180); //this line
Add(datePickerView);

NativeScript android play sound on notification

I'm writing native Android code in a Nativescript Angular app, to display a notification with sound.
I followed the recommendations in this answer.
I have a sound file named a.mp3, in the following folder:
Here is the code to configure the sound of the notification:
const uri = new android.net.Uri.Builder()
.scheme(android.content.ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(application.android.nativeApp.getPackageName())
.appendPath("raw")
.appendPath("a.mp3")
.build();
const AudioAttributes = android.media.AudioAttributes;
const audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
Here is the code to display a notification:
const NOTIFICATION_ID = 234;
const CHANNEL_ID = "my_channel_01";
const name = "my notifications";
const Description = "some desc";
const title = "notif title";
const message = "This notification has been triggered by me";
const NotificationManager = android.app.NotificationManager;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
const importance = android.app.NotificationManager.IMPORTANCE_HIGH;
const mChannel = new android.app.NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setSound(uri, audioAttributes);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(android.graphics.Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern([100, 200, 300, 400, 500, 400, 300, 200, 400]);
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
///////// Create an activity on tap (intent)
const Intent = android.content.Intent;
const PendingIntent = android.app.PendingIntent;
const intent = new Intent(context, com.tns.NativeScriptActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
const pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
///////// PRESERVE NAVIGATION ACTIVITY. To start a "regular activity" from your notification, set up
///////// the PendingIntent using TaskStackBuilder so that it creates a new back stack as follows.
///////// SEE: https://developer.android.com/training/notify-user/navigation.html
const TaskStackBuilder = android.support.v4.app.TaskStackBuilder;
const resultIntent = new Intent(context, com.tns.NativeScriptActivity.class);
const stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(com.tns.NativeScriptActivity.class);
stackBuilder.addNextIntent(resultIntent);
///////// Creating a notification
var NotificationCompat = android.support.v4.app.NotificationCompat;
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSound(uri)
.setSmallIcon(android.R.drawable.btn_star_big_on)
.setContentTitle(title)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText("By default, the notification's text content is truncated to fit one line. If you want your notification to be longer, you can enable an expandable notification by adding a style template with setStyle(). For example, the following code creates a larger text area:")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder.build());
The notification indeed fires, but the sound file is not played.
Can anyone help to solve this, please?
I also tried using another approach of acquiring the mp3 file, as recommended here:
const uri = android.net.Uri.parse("android.resource://" + context.getPackageName() + "/raw/a.mp3");
But that didn't help either.
Did I put the 'a.mp3' sound file in the correct folder that is recognized by android?
Thanks
I found the solution for this. I'm writing answer so that others can learn from this question.
Regarding the question I asked - Did I put the 'a.mp3' sound file in the correct folder that is recognized by android?
The answer is yes. /App_Resources/Android/src/main/res/raw/ is where the above code will look for the sound file.
But, I needed to do 2 modifications:
A_ The code needs to be changed to not include the file extension:
const uri = new android.net.Uri.Builder()
.scheme(android.content.ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(application.android.nativeApp.getPackageName())
.appendPath("raw")
.appendPath("a") // Referring to a.mp3
.build();
and then the sound uri would be used like in the code in the question:
const mChannel = new android.app.NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setSound(uri, audioAttributes);
or
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSound(uri)
B_ I needed to tell webpack to pack the .mp3 files from /App_Resources/Android/src/main/res/raw/ and put it into the nativescript build. To do this, I had to add { from: { glob: "**/*.mp3" } }, to webpack.config.js:
// Copy assets to out dir. Add your own globs as needed.
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.png" } },
{ from: { glob: "**/*.mp3" } },
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
As webpack isn't configured to copy all files from App_Resources automatically. The same is true for any other file extension that you'd use in your project (for example: .jpg files).
More info here (Q&A on 'nativescript-audio' plugin's git).

Xamarin forms CrossMedia.Current asking permission

I am developing a xamarin forms application which access device camera and gallery to take pictures. I am using the following code
CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = true,
Name = string.Format(TDJGolfConstants.ProfileImageName, Guid.NewGuid()),
//AllowCropping = true,
//CompressionQuality = 60,
PhotoSize= PhotoSize.Medium,
But it is not asking the user for permission , and directly opens the camera and gallery. Is there anything to add for asking permisision?
I use CrossPermissions from the PermissionsPlugin for this
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Camera))
{
await UserNotificationService.Current.Show("Camera required", "justification");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera);

Dynamically display image in IOS xamarin - nitWithContentsOfFile:' method returned nil

I have a portable xamarin application with some "specific overrides" for android and ios.
I have a content page with image that need to be downloaded from server.
Since the image is not "public" on the server I cannot use a simple url to add the image to the Image control.
So my code is downloading the image, save it locally and then pass the image url to the Image control:
string filePath = GetThumbnailPath();
Image img = new Image
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
WidthRequest = 150,
HeightRequest = 150,
Source = FileImageSource.FromFile(filePath),
Margin = new Thickness(0, 10, 0, 10)
};
layout.Children.Add(img);
On android everything works great.
On IOS the image is saved to:
string directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(directory, "thumbnail.jpg");
but when the page is loaded the application crash with:
System.Exception: Could not initialize an instance of the type 'UIKit.UIImage': the native 'initWithContentsOfFile:' method returned nil.
I cannot change the image to be a bundled resource (its dynamic) and cannot make the server call public (its an handler require header parameters or cookies)
On iOS you can save image to local storage with this code:
var rootFolder = FileSystem.Current.LocalStorage;
var folder = await rootFolder.CreateFolderAsync("Images", CreationCollisionOption.OpenIfExists);
var file = await folder.CreateFileAsync("image.png", CreationCollisionOption.ReplaceExisting);
var httpClient = new HttpClient();
var buffer = await httpClient.GetByteArrayAsync(url);
using (Stream stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
{
stream.Write(buffer, 0, buffer.Length);
}
return file.Path;
Than, you can show that image with:
var imageSource = new FileImageSource { File = filePath};
MyPicture.Source = imageSource;
Where MyPicture is defined with:
<Image x:Name="MyPicture">

Text on image not appearing, in preview handler vs2005 .net2

hi i am trying to show image of my file in previwew pane i am able to display the image of my file but i am stuck in the part where i need write some text on the image before adding it to preview pane.
// create an image object, using the filename we just retrieved
String strImageFile = file.FullName.Substring(0, file.FullName.Length - 3) + "jpg";
//file.CreationTime.ToString();
//------------------------------------
//Load the Image to be written on.
Bitmap bitMapImage = new System.Drawing.Bitmap(strImageFile);
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString("AWESOME!", new Font("Arial", 20, FontStyle.Bold), Brushes.Black, new Point(100, 250));
//Save the new image to the response output stream.
bitMapImage.Save(strImageFile, ImageFormat.Png);
//------------------------------------
// Create a picture box control
PictureBox p = new PictureBox();
p.Dock = DockStyle.Fill;
p.Image = bitMapImage;
//p.Image = System.Drawing.Image.FromFile(strImageFile);
p.SizeMode = PictureBoxSizeMode.Zoom;
Controls.Add(p);
//graphicImage.Dispose();
//bitMapImage.Dispose();
Only the image appease and not the text, any idea what i might be missing.
thanks
Narrow it down too:
PictureBox p = new PictureBox();
// create an image object, using the filename we just retrieved
String strImageFile = file.FullName.Substring(0, file.FullName.Length - 3) + "jpg";
Bitmap bitMapImage = new System.Drawing.Bitmap(strImageFile);
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphicImage.DrawString("AWESOME!", new Font("Arial", 20, FontStyle.Bold), Brushes.Black, new Point(100, 250));
graphicImage.DrawImage(bitMapImage, new Rectangle(205, 0, 200, 200), 0, 0, bitMapImage.Width, bitMapImage.Height, GraphicsUnit.Pixel);
p.Image = bitMapImage;
p.Dock = DockStyle.Fill;
Controls.Add(p);
But i am getting an exception on

Resources