How to set "context" of an capacitor plugin to use UsageStatsManager - java

I'm trying to write a capacitor plugin following this explanation. The Plugin should make use of Androids usageStatsManager.
With the code further below, I'm getting the following error in Android Studio:
error: cannot find symbol
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
symbol: variable context
location: class echoPlugin
So it seems somehow I'm not using context correctly.
My code in echoPlugin.java:
package com.echo.capacitor;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import android.content.Context;
import android.util.Log;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import java.util.Calendar;
import java.util.List;
#CapacitorPlugin(name = "echo")
public class echoPlugin extends Plugin {
#PluginMethod
public void getStats(Context call) {
Log.i("Method: ", "getStats");
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long now = Calendar.getInstance().getTimeInMillis();
List<UsageStats> stats = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0, now);
JSObject ret = new JSObject();
ret.put("my Stats",stats);
call.resolve(ret);
}
}
I have to add that I'm a complete Java beginner. I'm normally doing Apps with Vue+Ionic+Capacitor, but because there is no implementation of the UsageStatsManager in Capacitor I'm trying to find a way to make it work.

Use getContext() instead of context

Related

ibm-odm:IlrJ2SESessionFactory cannot be resolved to a type

I am using the following code to create list of rules fires in ODM ,but eclipse is showing above compilation error.
package com.cper.brms.model.questions;
import ilog.rules.res.session.IlrSessionRequest;
import ilog.rules.res.session.IlrSessionResponse;
import ilog.rules.res.session.ruleset.IlrBusinessExecutionTrace;
import ilog.rules.res.session.ruleset.IlrExecutionTrace;
import ilog.rules.teamserver.auth.AuthenticationCredentials;
import ilog.rules.teamserver.model.IlrConnectException;
import ilog.rules.teamserver.model.IlrSession;
import ilog.rules.teamserver.model.IlrSessionFactory;
import java.util.List;
import java.util.Map;
public class RulesTrace<IlrStatelessSession>
{
IlrSessionFactory sessionFactory = new IlrJ2SESessionFactory();
IlrSessionRequest sessionRequest = sessionFactory.createRequest();
String rulesetPath = "/miniloanruleapp/miniloanrules";
}
sessionRequest.setRulesetPath(IlrPath.parsePath(rulesetPath));
sessionRequest.setTraceEnabled(true);
sessionRequest.getTraceFilter().setInfoAllFilters(true);
Map<String,Object> inputParameters = sessionRequest.getInputParameters();
inputParameters.put("loan", loan);
inputParameters.put("borrower", borrower);
IlrStatelessSession session = sessionFactory.createStatelessSession();
IlrSessionResponse response = session.execute(sessionRequest);
IlrExecutionTrace sessionTrace = response.getRulesetExecutionTrace();
int rulesNumber = sessionTrace.getTotalRulesFired();
IlrBusinessExecutionTrace execResult = new IlrBusinessExecutionTrace(response.getRulesetExecutionTrace());
List<String> rulesFired = execResult.getRuleFiredBusinessNames();
loan = (Loan) response.getOutputParameters().get("loan");
}
Do I need to create any customized code to create sessionFactory or am i missing any jar?
You are missing jrules-res-execution.jar from your project. Adding it should solve the problem.
Its the version mismatch causing the error.I have JAVAEE in my eclipse,but above code is for JavaSE. Changed the code to right version.

SdkManager class is not available in latest android SDK

Downloaded latest android Studio (android-studio-bundle-162.3871768-windows).
We were using com.android.sdklib.SdkManager class in our software but in latest Android Studio I'm not able to find the above mentioned class in any jar present inside the tools\lib folder.
Can anyone suggest what is the better alternative for this?
if you want to get a list of all the targets installed for knowledge, then you can just simply run the SDK manager. But since you want to call the getTargets() method, it means you need it for other purposes. check up the documentation on the android studio web page to find out if it the class you are searching for exists and the location of its jar file.
We can find the soure code of all the android classes in the below link.
https://javalibs.com/artifact/com.android.tools/sdklib?className=com.android.sdklib.tool.SdkManagerCli&source
SdkManagerCli class have equivalent method listPackages()which will list the packages.
We need to import sdklib-25.3.2.jar, repository-25.3.2.jar and common-25.3.2.jar to project.
Below is the working code for listing packages:-
import java.io.File;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.TreeSet;
import com.android.repository.Revision;
import com.android.repository.api.ConsoleProgressIndicator;
import com.android.repository.api.LocalPackage;
import com.android.repository.api.ProgressIndicator;
import com.android.repository.api.RepoManager;
import com.android.repository.impl.meta.RepositoryPackages;
import com.android.sdklib.repository.AndroidSdkHandler;
public class AndroidTesting {
public static void main(String[] args) {
listPackages();
}
private static void listPackages() {
AndroidSdkHandler mHandler = AndroidSdkHandler.getInstance(new
File("filePath")); //for eg:-sdk/platforms for API
ProgressIndicator progress = new ConsoleProgressIndicator();
RepoManager mRepoManager = mHandler.getSdkManager(progress);
mRepoManager.loadSynchronously(cacheExpirationMs, progress,
downloader, settings)(0, progress, null, null);
RepositoryPackages packages = mRepoManager.getPackages();
Collection<LocalPackage> locals = new TreeSet<LocalPackage>();
Collection<LocalPackage> localObsoletes = new
TreeSet<LocalPackage>();
for (LocalPackage local : packages.getLocalPackages().values()) {
if (local.obsolete()) {
localObsoletes.add(local);
} else {
locals.add(local);
}
Revision version = local.getVersion();
System.out.println(local.getDisplayName() + " "
+ local.getVersion() );
}
}
}

Error :- Activity Main can't be resolved or is not a field

I searched this error and tried all available solutions but still getting this error. I am new to android so please help to rectify and remove this error.
I am fully aware that this error is somewhat associated with .R file of android.
I am using eclipse and getting 2 error
1- "activity_main cannot be resolved or is not a field"
2- List is a raw type. Reference to generic type.
Following is MainActivity code. This code is written to calculate nearest located Latlong distance..
package com.example.latlong;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class MainActivity extends Activity {
Double latToCompare=0.0, longToCompare=0.0;
float shortestDistance = 0.0f;
Boolean isDistanceComparedFirstTime = true;
Boolean isFirstRowInCSV1 = true, isFirstRowInCSV2 = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /////// Error1 Line
createDirIfNotExists("/windows/Pictures/LatLongFiles");
String csvFilename1 = Environment.getExternalStorageDirectory()+"/windows/Pictures/LatLongFiles/Data.csv";
String csvFilename2 = Environment.getExternalStorageDirectory()+"/windows/Pictures/LatLongFiles/SourceFile.csv";
String csvFilename3 = Environment.getExternalStorageDirectory()+"/windows/Pictures/LatLongFiles/OutputFile.csv";
CSVReader csvReader1 = null;
CSVReader csvReader2 = null;
CSVWriter csvWriter3 = null;
List csvFile2List = null; ///////////// Error2 Line
I know this error is not new to you android guys but i tried everything and still stuck on this error.. Thanks in advance..
Removed import android.R; And clean and rebuild your project
import your_application_package_name.R;
Error 1 indicates that the build system cannot find a layout file called "activity_main.xml". Verify that you have such a layout file.
Error 2 is simply a warning that informs you that you are using a raw type rather than a generic type. Add a type parameter that indicates the type of objects that are to reside in your list, e.g.:
List<YourTypeHere> csvFile2List = null;
Delete import.android.R; and in place of it, add import com.example.latlong.R; if com.example.latlong is your package name.
Or if this is not your package name, see your manifest file for the package name. And add import YourPackageName.R; instead of import.android.R; where YourPackageName is the name of your package.

getting error in CODEC_ID_MPEG1VIDEO and PIX_FMT_YUV420P

I am trying to create a code which will convert images into video. I have surfed a lot and find some method to do it using javacv. now i am getting variable not found error in these lines.
recorder.setCodecID( CODEC_ID_MPEG1VIDEO);
recorder.setPixelFormat( PIX_FMT_YUV420P);
I have imported all the jar files into my library as well but still getting this error. Some quick help would be appreciated. I have gone through the below link for the importing the javacv into my workspace.
How to Import JavaCV libraries to Android Project
package com.example.photoaday;
import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvShowImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvWaitKey;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import com.googlecode.javacv.FFmpegFrameRecorder;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Recordvideo extends Activity {
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
opencv_core.IplImage img = cvLoadImage("/sdcard/folder/img1.jpg");
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/sdcard/folder/test.mpeg",200,150);
try {
recorder.setCodecID( CODEC_ID_MPEG1VIDEO);
recorder.setFrameRate(30);
recorder.setPixelFormat( PIX_FMT_YUV420P);
recorder.start();
for (int i=0;i<100;i++)
{
recorder.record(img);
}
recorder.stop();
}
catch (Exception e){
e.printStackTrace();
}
}
}
I assume that your compiler gives error as follows
error: ‘CODEC_ID_MPEG1VIDEO’ was not declared in this scope
error: ‘PIX_FMT_YUV420P’ was not declared in this scope
then solution for error is
change : ‘CODEC_ID_MPEG1VIDEO’ to 'AV_CODEC_ID_MPEG1VIDEO'
change : ‘PIX_FMT_YUV420P’ to 'AV_PIX_FMT_YUV420P'

xuggler error - "cannot find symbol : class VideoImage" - what am i missing?

I am using xuggler to play video files from my code and the following is a snippet from the main code:
This snippet produces an error :
//The window we'll draw the video on.
private static VideoImage mScreen = null;
private static void updateJavaWindow(BufferedImage javaImage)
{
mScreen.setImage(javaImage);
}
// Opens a Swing window on screen.
private static void openJavaWindow()
{
mScreen = new VideoImage();
}
The error that i get is : cannot find symbol : class VideoImage
The header files used are :
import java.awt.image.BufferedImage;
import com.xuggle.xuggler.Global;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;
import com.xuggle.xuggler.Utils;
Am i missing in some import statement ? If not , here are the libraries i am using apart from JDK :
What is the reason i am getting that error ?
VideoImage Javadoc
You are not importing the right class.
com.xuggle.xuggler.demos.VideoImage
It seems like you are already using an IDE. It should automatically tell you what import you are missing if the correct library is in the build path.
You need to import the VideoImage class.

Categories

Resources