Android different languages on concurrent threads - android-resources

In an Android app I am trying to use different languages at the same time on concurrent threads. One thread is the main UI thread, the other is a background email process sending emails to users in various languages. The problem is when I change the language on one thread it changes the language on the other thread and I get 'language leaks'. Here is the method used to change the language to use the correct resources:
public static Resources getResourcesByLocale(Locale locale, Context context) {
Resources res = context.getResources();
AssetManager am = res.getAssets();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration mainConfig = res.getConfiguration();
Configuration config = new Configuration();
config.setTo(mainConfig);
config.locale = locale;
return new Resources(am, dm, config);
}
This method changes the system wide configuration. So any other process using the configuration will inherit the resources determined by the locale. Is there a way to use multiple configurations one for each locale? I have looked exhaustively for a solution other than calling this method everytime we display text. We want to use this app in Europe and Middle East with about 27 languages.
Any help would be much appreciated.

Related

How to find the number of active threads in a hystrix thread pool?

I am trying to finetune hystrix threadpool core size and max size. For that I need to know and plot the number of active threads at anytime in the pool. Is there a way to do so?
Is this the right way?
HystrixThreadPoolKey hystrixThreadPoolKey = new HystrixThreadPoolKey() {
#Override
public String name() {
return threadPoolKey;
}
};
HystrixThreadPoolMetrics hystrixThreadPoolMetrics = HystrixThreadPoolMetrics.getInstance(hystrixThreadPoolKey);
log.info("Hystrix active threads: {}", hystrixThreadPoolMetrics.getCurrentActiveCount().toString());
I am not sure because when I use this I get active thread count as 0, when the corePoolSize setting is 10.
This code works fine (after putting a null check for the time till no request is made). But the right way should be to use Netflix's servo.
Netflix Announcement
How to Use
Quoting some part from the announcement:
Servo is designed to make it easy for developers to export metrics from their application code, register them with JMX, and publish them to external monitoring systems
(Also active thread pool size can be less than the core size)

How can I get Excel to close when I'm done with it?

This is in a COM API Word AddIn. And yes normally Hans Passant's advice to let .NET clean everything up works.
But it is not working for the following case - and I have tested running normally (no debugger) and have narrowed it down to this specific code:
private Chart chart;
private bool displayAlerts;
private Application xlApp;
Chart chart = myShape.Chart;
ChartData chartData = chart.ChartData;
chartData.Activate();
WorkbookData = (Workbook)chartData.Workbook;
xlApp = WorkbookData.Application;
displayAlerts = xlApp.DisplayAlerts;
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
WorksheetData = (Worksheet)WorkbookData.Worksheets[1];
WorksheetDataName = WorksheetData.Name;
WorksheetData.UsedRange.Clear();
// ... do a bunch of stuff including writing to the worksheet
xlApp.DisplayAlerts = displayAlerts;
WorkbookData.Close(true);
I think the problem is likely Word is giving me this workbook and so who knows what it is doing to instantiate Excel. But even after I exit Word, the Excel instance is still running.
Again, in Word (not Excel), accessing a chart object to update the data in the chart.
COM objects need to be released completely, else "orphaned" objects can keep an application in memory, even after the code that called it goes out-of-scope.
This particular case may be special (compared to other code you've used previously) due to using xlApp. By default, an Excel Application object is not needed or used when manipulating charts with the object model introduced in Office 2007 (I think it was). It's used in the code in the question in order to hide the Excel window, which is visible by default (and by design). But the object model isn't designed to handle cleaning that up - it assumes it isn't present...
In my tests, the object is released correctly when (referencing the code in the question):
All Excel objects are set to null in the reverse order they are instantiated, being sure to quit the Excel application before trying to set that object to null:
WorksheetData = null;
WorkbookData = null;
xlApp.Quit();
xlApp = null;
Then, C# has a tendency to create objects behind the scenes when COM dot-notation is used - these don't always get cleaned up (released) properly. So it's good practice to create an object for each level of the hierarchy being used. (Note: VBA doesn't have this problem, so any code picked up from a VBA example or the macro recorder needs to be re-worked in this respect.) From the code in the question this affects WorksheetData.UsedRange.Clear();
Excel.Range usedRng = WorksheetData.UsedRange;
usedRng.Clear();
usedRng = null;
And the standard clean up, to make sure everything is released at a predictable moment:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
When things like this crop up, I always refer to ".NET Development for Office" by Andrew Whitechapel. That's really "bare bones" and some of it no longer relevant, given the changes to C# over the years (to make it "easier to use" in the way VB.NET is "easier"). But how COM interacts with .NET hasn't changed, deep down...

How to set sequence numbers manually in QuickFixJ?

I'm acting as an acceptor. Is there a way to set sequence numbers manually?
The first idea I had, was to modify .seqnums files, but it does not work.
Google mentions existence of setNextSenderMsgSeqNum and setNextTargetMsgSeqNum methods, however I can't tell on which object I should call them (using quickfixj 1.4).
I'm aware that setting sequence numbers by hand is discouraged and there are bunch of flags like ResetOnLogon and ResetOnDisconnect, but I have no control over initiator and there are bunch of other acceptors (test-tools) which are using the same session.
Application myApp = new FIXSender();
settings = new SessionSettings(sessionConfig);
MessageFactory messageFactory = new MessageFactory();
MessageStoreFactory storeFactory = new FileStoreFactory(settings);
LogFactory logFactory = new FileLogFactory(settings);
Acceptor acceptor = new SocketAcceptor(myApp, storeFactory, settings, logFactory, messageFactory);
acceptor.start();
First of all you need to explore the quickfixJ code to see how it is done.
Secondly what is the reason to use such an old version of quickfixJ ? Why not upgrade to the most recent version.
Thirdly you should be very wary of changing sequence numbers if you don't understand properly how they are used in the communication. If you don't understand you are guaranteed to get into murky problems.
You can do something like
Session.lookupSession(sessionID).setNextSenderMsgSeqNum())
But before you do it, it is very important to understand how sequence numbers are used
You can set the FIX fields, override the toAdmin callback
#Override
public void toAdmin(Message message, SessionID sessionId) {
message.setBoolean(ResetSeqNumFlag.FIELD, true);
}

Multiple services using Firebase job dispatcher in android

For an android app, i want start three services using firebase job dispatcher on different days. Can I use same FirebaseDispatcher object for all the three jobs?. Meanwhile, How to maintain same FirebaseDispatcher object even though app is closed?. If i use static object it will be cleared if app crashes. So how to maintain my FirebaseDispatcher object for scheduling multiple services using same dispatcher object? or Can I create different FirebaseDispatcher object for different services? Is it good practice?
You can use the same FirebaseJobDispatcher for all Jobs.
When the application is closed, your Jobs will be executed. How? This is the Android OS concern.
You simply describe when and how your Jobs will be launched. It's all. Read the documentation again and see the comments in the source code of the library.
You can create the new FirebaseJobDispatcher for different Jobs.
Misapplication of static objects -- is bad practice.
So
FirebaseJobDispatcher dispatcher1 =
new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job job1 = dispatcher1.newJobBuilder()
.setService(YourService1.class)
.setTag(Const.JOB_TAG_1)
// more options to run
.build();
Job job2 = dispatcher1.newJobBuilder()
.setService(YourService2.class)
.setTag(Const.JOB_TAG_2)
// ...
.build();
dispatcher1.mustSchedule(job1);
FirebaseJobDispatcher dispatcher2 =
new FirebaseJobDispatcher(new GooglePlayDriver(context));
dispatcher2.mustSchedule(job2);
dispatcher1.cancel(Const.JOB_TAG_1);
dispatcher2.cancel(Const.JOB_TAG_2);

Application not detecting input language changes via Text Service Framework DLL

OK, I have been at this for a while ...
I am trying to track when user changes input languages from Language Bar.
I have a Text Service DLL - modeled from MSDN and WinSDK samples - that registers fine, and I can use the interfaces ITfActiveLanguageProfileNotifySink & ITfLanguageProfileNotifySink and see those events just fine.
I also have finally realized that when I change languages these events occur for the application/process that currently has focus.
What I need to do is to simply have these events able to callback to my own application, when it has the focus. I know I am missing something.
Any help here is appreciated.
Thanks.
I did some double-checking, and you should be able to create a thread manager object without implementing ITextStoreACP so long as you don't call ITfThreadMgr::Activate.
So, the code should look like:
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
ITfThreadMgr* pThreadMgr(NULL);
hr = CoCreateInstance(CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfThreadMgr, (LPVOID*) &pThreadMgr);
if (SUCCEEDED(hr))
{
ITfSource *pSource;
hr = pThreadMgr->QueryInterface(IID_ITfSource, (LPVOID*)&pSource);
if(SUCCEEDED(hr))
{
hr = pSource->AdviseSink(IID_ITfActiveLanguageProfileNotifySink,
(ITfActiveLanguageProfileNotifySink*)this,
&m_dwCookie);
pSource->Release();
}
}
}
Alternatively, you can use ITfLanguageProfileNotifySink - this interface is driven from the ItfInputProcessorProfiles object instead of ItfThreadMgr. There's a sample of how to set it up on the MSDN page for ItfLanguageProfileNotifySink.
For both objects, you need to keep the source object (ITfThreadMgr or ITfInputProcessorProfiles) as well as the sink object (what you implement) alive until your application exits.
Before your application exits, you need to remove the sink from the source object using ITfSource::UnadviseSink, and then release the source object (using Release). (You don't need to keep the ItfSource interface alive for the life of your application, though.)