YouTubeAndroidPlayerApi - Use URL instead of VideoID - android

I am using YouTubeAndroidPlayerApi - 1.2.1, to show Youtube videos in my application. I get the url's from backend and they come in different formats. For example,
public static final String DOMAIN_1 = "https://www.youtube.com/watch?v=";
public static final String DOMAIN_2 = "http://www.youtube.com/watch?v=";
public static final String DOMAIN_3 = "https://youtu.be/";
public static final String DOMAIN_4 = "http://www.youtube.com/embed/";
public static final String DOMAIN_5 = "https://www.youtube.com/embed/";
All the above urls have VideoID at the end, and I am able to get VideoID and use,
Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) activity,
Const.YouTube.API_KEY, videoId, 0,
true, true);
to play them. But my problem is, there are also urls such as,
https://www.youtube.com/watch?t=15&v=video_id
https://www.youtube.com/watch?v=video_id&list=PL1RpYLGwB6WM409EJBVM1MS9bs3httFse&index=1
and since there are other characters in it, I couldn't extract the videoID.
Is there a way I can use URL with createVideoIntent() or is there any other way i can use URLs to work with YouTubeAndroidPlayerApi.
[EDIT]
If i can't use URL, can someone help me with getting a regex, for extracting videoID from the above Urls?

Use regular expression to cut off the rest of the url

Related

What are some examples of MediaController commands?

Checking out the MediaController documentation, I noticed that there is a function called sendCommand(...), which requires three parameters:
command: String;
args: Bundle;
cb: ResultReceiver.
But examples of how to use that method are nowhere to be found.
What are the available MediaController#sendCommand(...) default commands and acceptable argument keys and values types?
For example, checking PlaybackState documentation, we can find a constant called ACTION_PLAY_FROM_MEDIA_ID which description is as follows:
Indicates this session supports the play from media id command
This leads us to think that MediaController#sendCommand(...) is able to change a MediaSession's current media by sending it the media ID. How can we do it?
It's known that Google Play Music App's MediaController shares its Media Queue through MediaController#getQueue function.
You can find Commands constants in MediaControllerCompat.
They actually are:
public static final String COMMAND_GET_EXTRA_BINDER =
"android.support.v4.media.session.command.GET_EXTRA_BINDER";
public static final String COMMAND_ADD_QUEUE_ITEM =
"android.support.v4.media.session.command.ADD_QUEUE_ITEM";
public static final String COMMAND_ADD_QUEUE_ITEM_AT =
"android.support.v4.media.session.command.ADD_QUEUE_ITEM_AT";
public static final String COMMAND_REMOVE_QUEUE_ITEM =
"android.support.v4.media.session.command.REMOVE_QUEUE_ITEM";
public static final String COMMAND_REMOVE_QUEUE_ITEM_AT =
"android.support.v4.media.session.command.REMOVE_QUEUE_ITEM_AT";
public static final String COMMAND_ARGUMENT_MEDIA_DESCRIPTION =
"android.support.v4.media.session.command.ARGUMENT_MEDIA_DESCRIPTION";
public static final String COMMAND_ARGUMENT_INDEX =
"android.support.v4.media.session.command.ARGUMENT_INDEX";
For some basic usage samples u can check out its methods, like:
#Override
public void removeQueueItem(MediaDescriptionCompat description) {
long flags = getFlags();
if ((flags & MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS) == 0) {
throw new UnsupportedOperationException(
"This session doesn't support queue management operations");
}
Bundle params = new Bundle();
params.putParcelable(COMMAND_ARGUMENT_MEDIA_DESCRIPTION, description);
sendCommand(COMMAND_REMOVE_QUEUE_ITEM, params, null);
}

MobileHub where to store API keys

I am building an app based on the mobile hub sample app. The sample-app has the API keys stored in a class file AWSconfiguration:
public class AWSConfiguration {
// AWS MobileHub user agent string
public static final String AWS_MOBILEHUB_USER_AGENT =
"MobileHub ********* aws-my-sample-app-android-v0.16";
// AMAZON COGNITO
public static final Regions AMAZON_COGNITO_REGION =
Regions.fromName("us-east-1");
public static String AMAZON_COGNITO_IDENTITY_POOL_ID = "us-east-************6";
// Google Client ID for Web application
public static String GOOGLE_CLIENT_ID ="";//"*********************.apps.googleusercontent.com";
public static final Regions AMAZON_DYNAMODB_REGION =
Regions.fromName("us-east-1");
public static String AMAZON_COGNITO_USER_POOL_ID = "************";
public static String AMAZON_COGNITO_USER_POOL_CLIENT_ID = "*************";
public static String AMAZON_COGNITO_USER_POOL_CLIENT_SECRET = "*************";
private static final AWSMobileHelperConfiguration helperConfiguration = new AWSMobileHelperConfiguration.Builder()
.withCognitoRegion(AMAZON_COGNITO_REGION)
.withCognitoIdentityPoolId(AMAZON_COGNITO_IDENTITY_POOL_ID)
.withCognitoUserPool(AMAZON_COGNITO_USER_POOL_ID,
AMAZON_COGNITO_USER_POOL_CLIENT_ID, AMAZON_COGNITO_USER_POOL_CLIENT_SECRET)
.build();
/**
* #return the configuration for AWSKit.
*/
public static AWSMobileHelperConfiguration getAWSMobileHelperConfiguration() {
return helperConfiguration;
}
}
It seems unsafe to store the client secret key this way. What are the risks?
I experiemnted with hiding the keys in JNI files but could not find the proper entry point in the activity to set the keys before they are called from the mobile helper.
Storing in clear text is generally a bad idea, as you guessed. You could use the android keystore, store it encrypted (the stronger the key, the better), obfuscate it with some unique identifier of your device, or access it via some API you control and secure. It's possible to use some other solution, or a combination of the above possibilities. The final decision comes down to you and what your app needs/abilities are, but there's a few ways to hide it.
SharedPreferences.Editor can be a solution.
Password or something like this are stored in SharedPreferences.

How can I load an image from a url into an android remote view for a widget

I have an image url I parse form json that I want to load into an android widget onto the homescreen. Right now I am trying to do it this way but its wrong:
ImageDownloadTask imageD = new ImageDownloadTask(image);
views.setImageViewBitmap(R.id.image, imageD.execute(image));
image is a string holding a url to an image that needs to be downloaded and I am trying to set it to R.id.image
I found another stack question and tried this as a result:
views.setBitmap(R.id.image, "setImageBitmap",BitmapFactory.decodeStream(new URL(image).openStream()));
And when I use that nothing in the app loads at all, none of the text views get set.
My third try was this:
//get beer data
JSONObject o = new JSONObject(result);
String name = getName(o);
String image = getImage(o);
String abv = getABV(o);
String ibu = getIBU(o);
String glass = getGlass(o);
String beerBreweryName = getBreweryName(o);
String beerBreweryStyle = getBreweryStyle(o);
String beerDescription = getDescription(o);
InputStream in = new java.net.URL(image).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
views.setTextViewText(R.id.beerTitle, name);
views.setTextViewText(R.id.beerBreweryName, beerBreweryName);
views.setTextViewText(R.id.beerStyleName, beerBreweryStyle);
views.setImageViewBitmap(R.id.image, bitmap);
This gave the same result as the last attempt, it would not even set any text views....
Just tried another attempt after one of the answers posted below:
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_test);
//get beer data
JSONObject o = new JSONObject(result);
String name = getName(o);
String imageURL = getImage(o);
String abv = getABV(o);
String ibu = getIBU(o);
String glass = getGlass(o);
String beerBreweryName = getBreweryName(o);
String beerBreweryStyle = getBreweryStyle(o);
String beerDescription = getDescription(o);
Log.d("widgetImage" , imageURL);
views.setImageViewUri(R.id.image, Uri.parse(imageURL));
views.setTextViewText(R.id.beerTitle, name);
views.setTextViewText(R.id.beerBreweryName, beerBreweryName);
views.setTextViewText(R.id.beerStyleName, beerBreweryStyle);
mgr.updateAppWidget(appWidgetIds, views);
This attempt lets all the text views load, but no image ever shows up.
The way to do this reliably is to use setImageViewURI on the remote ImageView. The trick is that the URI you give it is a content:// URI which then points back to a content provider that you export from your application. In your content provider you can do anything you need to do to supply the image bytes.
For example, in your manifest:
<provider android:name=".ImageContentProvider" android:authorities="com.example.test" android:exported="true" />
And your provider:
public class ImageContentProvider extends ContentProvider {
// (Removed overrides that do nothing)
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
List<String> segs = uri.getPathSegments();
// Download the image content here, get the info you need from segs
return ParcelFileDescriptor.open(new File(path), ParcelFileDescriptor.MODE_READ_ONLY);
}
}
And then your URL is something like:
content://com.example.test/something-you-can-define/here
This is necessary because your remote image view is not running in your process. You are much more limited in what you can do because everything must be serialized across the process boundary. The URI can serialize just fine but if you try to send a megabyte of image data with setImageViewBitmap, it's probably going to fail (depending on available device memory).
Got a lot of help from multiple sources for this question. The big problem for me why a bunch of the attempts I tried listed above seemed to lock the widget app and not load anything is because I can not download the image and set it in a UI thread.
To accomplish this I had to move everything to the do in background of my async task and not in the onPostExecute.

extract substring from extras and append to another variable

After days of research and numerous compiler errors... i think i need some assitance from the community.
I have a simple app that retrieves a url through a shared intent from another app.
I want to extract a value from the intent's stringextra (which is a url) add it to the end of my script's url and send that final string as a URL to an URLconnection httpget request.
I've defined my script's url as a variable.
I've gotten the intent and its extras and put it in a variable.
I've tried to just concat my url variable and the stringextra variable to no avail,
URLconnectio gave me back a MalformedURLException. I've logged the concat'd variable
and it looked exactly like what i wanted but for some reason it wouldn't go through.
So now, I'm attempting to just cut the piece I need off of the stringextra and put it on my script's url and then make the http get request off that newly formed string.
SO....
how do I extract 11 characters after "v=", which is in the stringextra, which is
a variable called sharedText?
String goLoQooUrl = "http://my.domain.my/script.php?link=";
private static final String TAG = "LTV";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent rIntent = getIntent();
String rAction = rIntent.getAction();
String rType = rIntent.getType();
if (Intent.ACTION_SEND.equals(rAction) && "text/plain".equals(rType))
displaySentText(rIntent);
}
private void displaySentText (Intent rIntent) {
String sharedText = rIntent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText == null) return;
if (sharedText.startsWith("http://")) {
TextView url = (TextView) findViewById(R.id.URL);
url.setText(String.valueOf(sharedText));
****String w = goLoQooUrl+SUBSTRING***** this is what I want
Log.d("TAG", w);
};
playOnLoqooTv(w);
}
private void playOnLoqooTv (String w) {
try {
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url = new URL(w);
I've tried numerous things to no avail
namely tried using the substring method and it gave me an error about that method
wasn't available I probably wasn't calling it correct.
Can any Masters of the Universes', shed some light?
Try this out:-
int indexOfv = sharedText.indexOf("="); // Get the index of =
indexOfv++; // To get the substring after this element.
String subString = sharedText.substring(indexOfv, indexOfv+11); //Get the 11 characters after the = sign.

i want to retrieve json data via url with android

i'm working on a API, and i want to take the json data that is display in an adress like this : https://api.empireavenue.com/profile/info/?apikey=YOURAPIKEY&username=TICKER&password=PASSWORD
and take this data to display it on my app .
Thanks.
The input to the parseJson method should be whatever you get back from the HTTP request. This also assumes the format is something like:
{"rootKey":{"intVal":1}}
You will have to modify the order in which you query objects depending on the structure.
private static final ROOT_JSON_KEY = "rootKey";
private static final INT_VALUE_NAME = "intVal";
public void parseJson(String webServiceResponse) {
JSONObject json = new JSONObject(webServiceResponse);
JSONObject rootObject = json.getJSONObject(ROOT_KEY);
int intValues = rootObject.getInt(INT_VALUE_NAME);
}
EDIT: Sorry, stupid coding error.

Categories

Resources